@@ -3552,24 +3552,23 @@ which references a URL (e.g., a path prefixed with `classpath:`, `file:`, `http:
3552
3552
will be loaded using the specified resource protocol.
3553
3553
3554
3554
The following example demonstrates how to use `@Sql` at the class level and at the method
3555
- level within a JUnit 4 based integration test class.
3555
+ level within a JUnit Jupiter based integration test class.
3556
3556
3557
3557
[source,java,indent=0]
3558
3558
[subs="verbatim,quotes"]
3559
3559
----
3560
- @RunWith(SpringRunner.class)
3561
- @ContextConfiguration
3560
+ @SpringJUnitConfig
3562
3561
@Sql("/test-schema.sql")
3563
- public class DatabaseTests {
3562
+ class DatabaseTests {
3564
3563
3565
3564
@Test
3566
- public void emptySchemaTest {
3565
+ void emptySchemaTest {
3567
3566
// execute code that uses the test schema without any test data
3568
3567
}
3569
3568
3570
3569
@Test
3571
3570
@Sql({"/test-schema.sql", "/test-user-data.sql"})
3572
- public void userTest {
3571
+ void userTest {
3573
3572
// execute code that uses the test schema and test data
3574
3573
}
3575
3574
}
@@ -3699,41 +3698,41 @@ executed in an isolated transaction. Although a thorough discussion of all suppo
3699
3698
options for transaction management with `@Sql` is beyond the scope of this reference
3700
3699
manual, the javadocs for `@SqlConfig` and `SqlScriptsTestExecutionListener` provide
3701
3700
detailed information, and the following example demonstrates a typical testing scenario
3702
- using JUnit 4 and transactional tests with `@Sql`. Note that there is no need to clean up
3703
- the database after the `usersTest()` method is executed since any changes made to the
3704
- database (either within the test method or within the `/test-data.sql` script) will
3705
- be automatically rolled back by the `TransactionalTestExecutionListener` (see
3701
+ using JUnit Jupiter and transactional tests with `@Sql`. Note that there is no need to
3702
+ clean up the database after the `usersTest()` method is executed since any changes made
3703
+ to the database (either within the test method or within the `/test-data.sql` script)
3704
+ will be automatically rolled back by the `TransactionalTestExecutionListener` (see
3706
3705
<<testcontext-tx,transaction management>> for details).
3707
3706
3708
3707
[source,java,indent=0]
3709
3708
[subs="verbatim,quotes"]
3710
3709
----
3711
- @RunWith(SpringRunner.class)
3712
- @ContextConfiguration(classes = TestDatabaseConfig.class)
3710
+ @SpringJUnitConfig(TestDatabaseConfig.class)
3713
3711
@Transactional
3714
- public class TransactionalSqlScriptsTests {
3712
+ class TransactionalSqlScriptsTests {
3715
3713
3716
- protected JdbcTemplate jdbcTemplate;
3714
+ final JdbcTemplate jdbcTemplate;
3717
3715
3718
3716
@Autowired
3719
- public void setDataSource (DataSource dataSource) {
3717
+ TransactionalSqlScriptsTests (DataSource dataSource) {
3720
3718
this.jdbcTemplate = new JdbcTemplate(dataSource);
3721
3719
}
3722
3720
3723
3721
@Test
3724
3722
@Sql("/test-data.sql")
3725
- public void usersTest() {
3723
+ void usersTest() {
3726
3724
// verify state in test database:
3727
3725
assertNumUsers(2);
3728
3726
// execute code that uses the test data...
3729
3727
}
3730
3728
3731
- protected int countRowsInTable(String tableName) {
3729
+ int countRowsInTable(String tableName) {
3732
3730
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
3733
3731
}
3734
3732
3735
- protected void assertNumUsers(int expected) {
3736
- assertEquals("Number of rows in the [user] table.", expected, countRowsInTable("user"));
3733
+ void assertNumUsers(int expected) {
3734
+ assertEquals(expected, countRowsInTable("user"),
3735
+ "Number of rows in the [user] table.");
3737
3736
}
3738
3737
}
3739
3738
----
@@ -4095,9 +4094,8 @@ use of `@RepeatedTest` from JUnit Jupiter allows the test method to gain access
4095
4094
class OrderServiceIntegrationTests {
4096
4095
4097
4096
@RepeatedTest(10)
4098
- void placeOrderRepeatedly(
4099
- @Autowired OrderService orderService,
4100
- RepetitionInfo repetitionInfo) {
4097
+ void placeOrderRepeatedly(RepetitionInfo repetitionInfo,
4098
+ @Autowired OrderService orderService) {
4101
4099
4102
4100
// use orderService from the test's ApplicationContext
4103
4101
// and repetitionInfo from JUnit Jupiter
@@ -4194,37 +4192,33 @@ of the Servlet API>> available in the `spring-test` module. This allows performi
4194
4192
requests and generating responses without the need for running in a Servlet container.
4195
4193
For the most part everything should work as it does at runtime with a few notable
4196
4194
exceptions as explained in <<spring-mvc-test-vs-end-to-end-integration-tests>>. Here is a
4197
- JUnit 4 based example of using Spring MVC Test:
4195
+ JUnit Jupiter based example of using Spring MVC Test:
4198
4196
4199
4197
[source,java,indent=0]
4200
4198
----
4201
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
4202
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
4199
+ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
4200
+ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
4203
4201
4204
- @RunWith(SpringRunner.class)
4205
- @WebAppConfiguration
4206
- @ContextConfiguration("test-servlet-context.xml")
4207
- public class ExampleTests {
4202
+ @SpringJUnitWebConfig(locations = "test-servlet-context.xml")
4203
+ class ExampleTests {
4208
4204
4209
- @Autowired
4210
- private WebApplicationContext wac;
4205
+ private MockMvc mockMvc;
4211
4206
4212
- private MockMvc mockMvc;
4213
-
4214
- @Before
4215
- public void setup() {
4216
- this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
4217
- }
4207
+ @BeforeEach
4208
+ void setup(WebApplicationContext wac) {
4209
+ this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
4210
+ }
4218
4211
4219
- @Test
4220
- public void getAccount() throws Exception {
4221
- this.mockMvc.perform(get("/accounts/1").accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
4222
- .andExpect(status().isOk())
4223
- .andExpect(content().contentType("application/json"))
4224
- .andExpect(jsonPath("$.name").value("Lee"));
4225
- }
4212
+ @Test
4213
+ void getAccount() throws Exception {
4214
+ this.mockMvc.perform(get("/accounts/1")
4215
+ .accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
4216
+ .andExpect(status().isOk())
4217
+ .andExpect(content().contentType("application/json"))
4218
+ .andExpect(jsonPath("$.name").value("Lee"));
4219
+ }
4226
4220
4227
- }
4221
+ }
4228
4222
----
4229
4223
4230
4224
The above test relies on the `WebApplicationContext` support of the __TestContext framework__
0 commit comments