Skip to content

Commit d3129a8

Browse files
committed
Convert selected examples to JUnit Jupiter in reference manual
Issue: SPR-14524
1 parent 8c9d42f commit d3129a8

File tree

1 file changed

+39
-45
lines changed

1 file changed

+39
-45
lines changed

src/docs/asciidoc/testing.adoc

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3552,24 +3552,23 @@ which references a URL (e.g., a path prefixed with `classpath:`, `file:`, `http:
35523552
will be loaded using the specified resource protocol.
35533553

35543554
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.
35563556

35573557
[source,java,indent=0]
35583558
[subs="verbatim,quotes"]
35593559
----
3560-
@RunWith(SpringRunner.class)
3561-
@ContextConfiguration
3560+
@SpringJUnitConfig
35623561
@Sql("/test-schema.sql")
3563-
public class DatabaseTests {
3562+
class DatabaseTests {
35643563
35653564
@Test
3566-
public void emptySchemaTest {
3565+
void emptySchemaTest {
35673566
// execute code that uses the test schema without any test data
35683567
}
35693568
35703569
@Test
35713570
@Sql({"/test-schema.sql", "/test-user-data.sql"})
3572-
public void userTest {
3571+
void userTest {
35733572
// execute code that uses the test schema and test data
35743573
}
35753574
}
@@ -3699,41 +3698,41 @@ executed in an isolated transaction. Although a thorough discussion of all suppo
36993698
options for transaction management with `@Sql` is beyond the scope of this reference
37003699
manual, the javadocs for `@SqlConfig` and `SqlScriptsTestExecutionListener` provide
37013700
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
37063705
<<testcontext-tx,transaction management>> for details).
37073706

37083707
[source,java,indent=0]
37093708
[subs="verbatim,quotes"]
37103709
----
3711-
@RunWith(SpringRunner.class)
3712-
@ContextConfiguration(classes = TestDatabaseConfig.class)
3710+
@SpringJUnitConfig(TestDatabaseConfig.class)
37133711
@Transactional
3714-
public class TransactionalSqlScriptsTests {
3712+
class TransactionalSqlScriptsTests {
37153713
3716-
protected JdbcTemplate jdbcTemplate;
3714+
final JdbcTemplate jdbcTemplate;
37173715
37183716
@Autowired
3719-
public void setDataSource(DataSource dataSource) {
3717+
TransactionalSqlScriptsTests(DataSource dataSource) {
37203718
this.jdbcTemplate = new JdbcTemplate(dataSource);
37213719
}
37223720
37233721
@Test
37243722
@Sql("/test-data.sql")
3725-
public void usersTest() {
3723+
void usersTest() {
37263724
// verify state in test database:
37273725
assertNumUsers(2);
37283726
// execute code that uses the test data...
37293727
}
37303728
3731-
protected int countRowsInTable(String tableName) {
3729+
int countRowsInTable(String tableName) {
37323730
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
37333731
}
37343732
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.");
37373736
}
37383737
}
37393738
----
@@ -4095,9 +4094,8 @@ use of `@RepeatedTest` from JUnit Jupiter allows the test method to gain access
40954094
class OrderServiceIntegrationTests {
40964095
40974096
@RepeatedTest(10)
4098-
void placeOrderRepeatedly(
4099-
@Autowired OrderService orderService,
4100-
RepetitionInfo repetitionInfo) {
4097+
void placeOrderRepeatedly(RepetitionInfo repetitionInfo,
4098+
@Autowired OrderService orderService) {
41014099
41024100
// use orderService from the test's ApplicationContext
41034101
// and repetitionInfo from JUnit Jupiter
@@ -4194,37 +4192,33 @@ of the Servlet API>> available in the `spring-test` module. This allows performi
41944192
requests and generating responses without the need for running in a Servlet container.
41954193
For the most part everything should work as it does at runtime with a few notable
41964194
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:
41984196

41994197
[source,java,indent=0]
42004198
----
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.*;
42034201
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 {
42084204
4209-
@Autowired
4210-
private WebApplicationContext wac;
4205+
private MockMvc mockMvc;
42114206
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+
}
42184211
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+
}
42264220
4227-
}
4221+
}
42284222
----
42294223

42304224
The above test relies on the `WebApplicationContext` support of the __TestContext framework__

0 commit comments

Comments
 (0)