Convert selected examples to JUnit Jupiter in reference manual
Issue: SPR-14524
This commit is contained in:
@@ -3552,24 +3552,23 @@ which references a URL (e.g., a path prefixed with `classpath:`, `file:`, `http:
|
|||||||
will be loaded using the specified resource protocol.
|
will be loaded using the specified resource protocol.
|
||||||
|
|
||||||
The following example demonstrates how to use `@Sql` at the class level and at the method
|
The following example demonstrates how to use `@Sql` at the class level and at the method
|
||||||
level within a JUnit 4 based integration test class.
|
level within a JUnit Jupiter based integration test class.
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
@RunWith(SpringRunner.class)
|
@SpringJUnitConfig
|
||||||
@ContextConfiguration
|
|
||||||
@Sql("/test-schema.sql")
|
@Sql("/test-schema.sql")
|
||||||
public class DatabaseTests {
|
class DatabaseTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void emptySchemaTest {
|
void emptySchemaTest {
|
||||||
// execute code that uses the test schema without any test data
|
// execute code that uses the test schema without any test data
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Sql({"/test-schema.sql", "/test-user-data.sql"})
|
@Sql({"/test-schema.sql", "/test-user-data.sql"})
|
||||||
public void userTest {
|
void userTest {
|
||||||
// execute code that uses the test schema and test data
|
// execute code that uses the test schema and test data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3699,41 +3698,41 @@ executed in an isolated transaction. Although a thorough discussion of all suppo
|
|||||||
options for transaction management with `@Sql` is beyond the scope of this reference
|
options for transaction management with `@Sql` is beyond the scope of this reference
|
||||||
manual, the javadocs for `@SqlConfig` and `SqlScriptsTestExecutionListener` provide
|
manual, the javadocs for `@SqlConfig` and `SqlScriptsTestExecutionListener` provide
|
||||||
detailed information, and the following example demonstrates a typical testing scenario
|
detailed information, and the following example demonstrates a typical testing scenario
|
||||||
using JUnit 4 and transactional tests with `@Sql`. Note that there is no need to clean up
|
using JUnit Jupiter and transactional tests with `@Sql`. Note that there is no need to
|
||||||
the database after the `usersTest()` method is executed since any changes made to the
|
clean up the database after the `usersTest()` method is executed since any changes made
|
||||||
database (either within the test method or within the `/test-data.sql` script) will
|
to the database (either within the test method or within the `/test-data.sql` script)
|
||||||
be automatically rolled back by the `TransactionalTestExecutionListener` (see
|
will be automatically rolled back by the `TransactionalTestExecutionListener` (see
|
||||||
<<testcontext-tx,transaction management>> for details).
|
<<testcontext-tx,transaction management>> for details).
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
@RunWith(SpringRunner.class)
|
@SpringJUnitConfig(TestDatabaseConfig.class)
|
||||||
@ContextConfiguration(classes = TestDatabaseConfig.class)
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public class TransactionalSqlScriptsTests {
|
class TransactionalSqlScriptsTests {
|
||||||
|
|
||||||
protected JdbcTemplate jdbcTemplate;
|
final JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void setDataSource(DataSource dataSource) {
|
TransactionalSqlScriptsTests(DataSource dataSource) {
|
||||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Sql("/test-data.sql")
|
@Sql("/test-data.sql")
|
||||||
public void usersTest() {
|
void usersTest() {
|
||||||
// verify state in test database:
|
// verify state in test database:
|
||||||
assertNumUsers(2);
|
assertNumUsers(2);
|
||||||
// execute code that uses the test data...
|
// execute code that uses the test data...
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int countRowsInTable(String tableName) {
|
int countRowsInTable(String tableName) {
|
||||||
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
|
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void assertNumUsers(int expected) {
|
void assertNumUsers(int expected) {
|
||||||
assertEquals("Number of rows in the [user] table.", expected, countRowsInTable("user"));
|
assertEquals(expected, countRowsInTable("user"),
|
||||||
|
"Number of rows in the [user] table.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
@@ -4095,9 +4094,8 @@ use of `@RepeatedTest` from JUnit Jupiter allows the test method to gain access
|
|||||||
class OrderServiceIntegrationTests {
|
class OrderServiceIntegrationTests {
|
||||||
|
|
||||||
@RepeatedTest(10)
|
@RepeatedTest(10)
|
||||||
void placeOrderRepeatedly(
|
void placeOrderRepeatedly(RepetitionInfo repetitionInfo,
|
||||||
@Autowired OrderService orderService,
|
@Autowired OrderService orderService) {
|
||||||
RepetitionInfo repetitionInfo) {
|
|
||||||
|
|
||||||
// use orderService from the test's ApplicationContext
|
// use orderService from the test's ApplicationContext
|
||||||
// and repetitionInfo from JUnit Jupiter
|
// and repetitionInfo from JUnit Jupiter
|
||||||
@@ -4194,37 +4192,33 @@ of the Servlet API>> available in the `spring-test` module. This allows performi
|
|||||||
requests and generating responses without the need for running in a Servlet container.
|
requests and generating responses without the need for running in a Servlet container.
|
||||||
For the most part everything should work as it does at runtime with a few notable
|
For the most part everything should work as it does at runtime with a few notable
|
||||||
exceptions as explained in <<spring-mvc-test-vs-end-to-end-integration-tests>>. Here is a
|
exceptions as explained in <<spring-mvc-test-vs-end-to-end-integration-tests>>. Here is a
|
||||||
JUnit 4 based example of using Spring MVC Test:
|
JUnit Jupiter based example of using Spring MVC Test:
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
----
|
----
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@SpringJUnitWebConfig(locations = "test-servlet-context.xml")
|
||||||
@WebAppConfiguration
|
class ExampleTests {
|
||||||
@ContextConfiguration("test-servlet-context.xml")
|
|
||||||
public class ExampleTests {
|
|
||||||
|
|
||||||
@Autowired
|
private MockMvc mockMvc;
|
||||||
private WebApplicationContext wac;
|
|
||||||
|
|
||||||
private MockMvc mockMvc;
|
@BeforeEach
|
||||||
|
void setup(WebApplicationContext wac) {
|
||||||
|
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
|
||||||
|
}
|
||||||
|
|
||||||
@Before
|
@Test
|
||||||
public void setup() {
|
void getAccount() throws Exception {
|
||||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
this.mockMvc.perform(get("/accounts/1")
|
||||||
}
|
.accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().contentType("application/json"))
|
||||||
|
.andExpect(jsonPath("$.name").value("Lee"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
}
|
||||||
public void getAccount() throws Exception {
|
|
||||||
this.mockMvc.perform(get("/accounts/1").accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andExpect(content().contentType("application/json"))
|
|
||||||
.andExpect(jsonPath("$.name").value("Lee"));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
----
|
----
|
||||||
|
|
||||||
The above test relies on the `WebApplicationContext` support of the __TestContext framework__
|
The above test relies on the `WebApplicationContext` support of the __TestContext framework__
|
||||||
|
|||||||
Reference in New Issue
Block a user