|
|
@ -6809,8 +6809,12 @@ application. Test support is provided by two modules: `spring-boot-test` contain
|
|
|
|
items, and `spring-boot-test-autoconfigure` supports auto-configuration for tests.
|
|
|
|
items, and `spring-boot-test-autoconfigure` supports auto-configuration for tests.
|
|
|
|
|
|
|
|
|
|
|
|
Most developers use the `spring-boot-starter-test` "`Starter`", which imports both Spring
|
|
|
|
Most developers use the `spring-boot-starter-test` "`Starter`", which imports both Spring
|
|
|
|
Boot test modules as well as JUnit, AssertJ, Hamcrest, and a number of other useful
|
|
|
|
Boot test modules as well as JUnit Jupiter, AssertJ, Hamcrest, and a number of other
|
|
|
|
libraries.
|
|
|
|
useful libraries.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TIP: The starter brings also the vintage engine so that you can run both JUnit 4 and Junit
|
|
|
|
|
|
|
|
5 tests. If you have migrated your tests to JUnit 5, consider excluding the vintage
|
|
|
|
|
|
|
|
engine.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -6819,7 +6823,8 @@ libraries.
|
|
|
|
The `spring-boot-starter-test` "`Starter`" (in the `test` `scope`) contains
|
|
|
|
The `spring-boot-starter-test` "`Starter`" (in the `test` `scope`) contains
|
|
|
|
the following provided libraries:
|
|
|
|
the following provided libraries:
|
|
|
|
|
|
|
|
|
|
|
|
* https://junit.org[JUnit 4]: The de-facto standard for unit testing Java applications.
|
|
|
|
* https://junit.org/junit5[JUnit 5] (including the vintage engine for backward
|
|
|
|
|
|
|
|
compatibility with JUnit 4: The de-facto standard for unit testing Java applications.
|
|
|
|
* {spring-reference}testing.html#integration-testing[Spring Test] & Spring Boot Test:
|
|
|
|
* {spring-reference}testing.html#integration-testing[Spring Test] & Spring Boot Test:
|
|
|
|
Utilities and integration test support for Spring Boot applications.
|
|
|
|
Utilities and integration test support for Spring Boot applications.
|
|
|
|
* https://joel-costigliola.github.io/assertj/[AssertJ]: A fluent assertion library.
|
|
|
|
* https://joel-costigliola.github.io/assertj/[AssertJ]: A fluent assertion library.
|
|
|
@ -7115,19 +7120,16 @@ implementation:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.*;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.runner.*;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.*;
|
|
|
|
import org.springframework.beans.factory.annotation.*;
|
|
|
|
import org.springframework.boot.test.context.*;
|
|
|
|
import org.springframework.boot.test.context.*;
|
|
|
|
import org.springframework.boot.test.mock.mockito.*;
|
|
|
|
import org.springframework.boot.test.mock.mockito.*;
|
|
|
|
import org.springframework.test.context.junit4.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.*;
|
|
|
|
import static org.assertj.core.api.Assertions.*;
|
|
|
|
import static org.mockito.BDDMockito.*;
|
|
|
|
import static org.mockito.BDDMockito.*;
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@SpringBootTest
|
|
|
|
@SpringBootTest
|
|
|
|
public class MyTests {
|
|
|
|
class MyTests {
|
|
|
|
|
|
|
|
|
|
|
|
@MockBean
|
|
|
|
@MockBean
|
|
|
|
private RemoteService remoteService;
|
|
|
|
private RemoteService remoteService;
|
|
|
@ -7136,7 +7138,7 @@ implementation:
|
|
|
|
private Reverser reverser;
|
|
|
|
private Reverser reverser;
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void exampleTest() {
|
|
|
|
void exampleTest() {
|
|
|
|
// RemoteService has been injected into the reverser bean
|
|
|
|
// RemoteService has been injected into the reverser bean
|
|
|
|
given(this.remoteService.someCall()).willReturn("mock");
|
|
|
|
given(this.remoteService.someCall()).willReturn("mock");
|
|
|
|
String reverse = reverser.reverseSomeCall();
|
|
|
|
String reverse = reverser.reverseSomeCall();
|
|
|
@ -7219,25 +7221,22 @@ Strings respectively. Any helper fields on the test class can be `@Autowired` wh
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.*;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.runner.*;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.*;
|
|
|
|
import org.springframework.beans.factory.annotation.*;
|
|
|
|
import org.springframework.boot.test.autoconfigure.json.*;
|
|
|
|
import org.springframework.boot.test.autoconfigure.json.*;
|
|
|
|
import org.springframework.boot.test.context.*;
|
|
|
|
import org.springframework.boot.test.context.*;
|
|
|
|
import org.springframework.boot.test.json.*;
|
|
|
|
import org.springframework.boot.test.json.*;
|
|
|
|
import org.springframework.test.context.junit4.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.*;
|
|
|
|
import static org.assertj.core.api.Assertions.*;
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@JsonTest
|
|
|
|
@JsonTest
|
|
|
|
public class MyJsonTests {
|
|
|
|
class MyJsonTests {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private JacksonTester<VehicleDetails> json;
|
|
|
|
private JacksonTester<VehicleDetails> json;
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testSerialize() throws Exception {
|
|
|
|
void testSerialize() throws Exception {
|
|
|
|
VehicleDetails details = new VehicleDetails("Honda", "Civic");
|
|
|
|
VehicleDetails details = new VehicleDetails("Honda", "Civic");
|
|
|
|
// Assert against a `.json` file in the same package as the test
|
|
|
|
// Assert against a `.json` file in the same package as the test
|
|
|
|
assertThat(this.json.write(details)).isEqualToJson("expected.json");
|
|
|
|
assertThat(this.json.write(details)).isEqualToJson("expected.json");
|
|
|
@ -7303,8 +7302,7 @@ uses `MockMvc`:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.*;
|
|
|
|
import org.junit.jupiter.api.*;
|
|
|
|
import org.junit.runner.*;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.*;
|
|
|
|
import org.springframework.beans.factory.annotation.*;
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.*;
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.*;
|
|
|
|
import org.springframework.boot.test.mock.mockito.*;
|
|
|
|
import org.springframework.boot.test.mock.mockito.*;
|
|
|
@ -7314,9 +7312,8 @@ uses `MockMvc`:
|
|
|
|
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)
|
|
|
|
|
|
|
|
@WebMvcTest(UserVehicleController.class)
|
|
|
|
@WebMvcTest(UserVehicleController.class)
|
|
|
|
public class MyControllerTests {
|
|
|
|
class MyControllerTests {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private MockMvc mvc;
|
|
|
|
private MockMvc mvc;
|
|
|
@ -7325,7 +7322,7 @@ uses `MockMvc`:
|
|
|
|
private UserVehicleService userVehicleService;
|
|
|
|
private UserVehicleService userVehicleService;
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testExample() throws Exception {
|
|
|
|
void testExample() throws Exception {
|
|
|
|
given(this.userVehicleService.getVehicleDetails("sboot"))
|
|
|
|
given(this.userVehicleService.getVehicleDetails("sboot"))
|
|
|
|
.willReturn(new VehicleDetails("Honda", "Civic"));
|
|
|
|
.willReturn(new VehicleDetails("Honda", "Civic"));
|
|
|
|
this.mvc.perform(get("/sboot/vehicle").accept(MediaType.TEXT_PLAIN))
|
|
|
|
this.mvc.perform(get("/sboot/vehicle").accept(MediaType.TEXT_PLAIN))
|
|
|
@ -7346,8 +7343,7 @@ bean and/or a `WebDriver` bean. The following example uses HtmlUnit:
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import com.gargoylesoftware.htmlunit.*;
|
|
|
|
import com.gargoylesoftware.htmlunit.*;
|
|
|
|
import org.junit.*;
|
|
|
|
import org.junit.jupiter.api.*;
|
|
|
|
import org.junit.runner.*;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.*;
|
|
|
|
import org.springframework.beans.factory.annotation.*;
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.*;
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.*;
|
|
|
|
import org.springframework.boot.test.mock.mockito.*;
|
|
|
|
import org.springframework.boot.test.mock.mockito.*;
|
|
|
@ -7355,9 +7351,8 @@ bean and/or a `WebDriver` bean. The following example uses HtmlUnit:
|
|
|
|
import static org.assertj.core.api.Assertions.*;
|
|
|
|
import static org.assertj.core.api.Assertions.*;
|
|
|
|
import static org.mockito.BDDMockito.*;
|
|
|
|
import static org.mockito.BDDMockito.*;
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@WebMvcTest(UserVehicleController.class)
|
|
|
|
@WebMvcTest(UserVehicleController.class)
|
|
|
|
public class MyHtmlUnitTests {
|
|
|
|
class MyHtmlUnitTests {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private WebClient webClient;
|
|
|
|
private WebClient webClient;
|
|
|
@ -7366,7 +7361,7 @@ bean and/or a `WebDriver` bean. The following example uses HtmlUnit:
|
|
|
|
private UserVehicleService userVehicleService;
|
|
|
|
private UserVehicleService userVehicleService;
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testExample() throws Exception {
|
|
|
|
void testExample() throws Exception {
|
|
|
|
given(this.userVehicleService.getVehicleDetails("sboot"))
|
|
|
|
given(this.userVehicleService.getVehicleDetails("sboot"))
|
|
|
|
.willReturn(new VehicleDetails("Honda", "Civic"));
|
|
|
|
.willReturn(new VehicleDetails("Honda", "Civic"));
|
|
|
|
HtmlPage page = this.webClient.getPage("/sboot/vehicle.html");
|
|
|
|
HtmlPage page = this.webClient.getPage("/sboot/vehicle.html");
|
|
|
@ -7425,18 +7420,15 @@ example shows a class that uses both `@WebFluxTest` and a `WebTestClient`:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
|
|
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@WebFluxTest(UserVehicleController.class)
|
|
|
|
@WebFluxTest(UserVehicleController.class)
|
|
|
|
public class MyControllerTests {
|
|
|
|
class MyControllerTests {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private WebTestClient webClient;
|
|
|
|
private WebTestClient webClient;
|
|
|
@ -7445,7 +7437,7 @@ example shows a class that uses both `@WebFluxTest` and a `WebTestClient`:
|
|
|
|
private UserVehicleService userVehicleService;
|
|
|
|
private UserVehicleService userVehicleService;
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testExample() throws Exception {
|
|
|
|
void testExample() throws Exception {
|
|
|
|
given(this.userVehicleService.getVehicleDetails("sboot"))
|
|
|
|
given(this.userVehicleService.getVehicleDetails("sboot"))
|
|
|
|
.willReturn(new VehicleDetails("Honda", "Civic"));
|
|
|
|
.willReturn(new VehicleDetails("Honda", "Civic"));
|
|
|
|
this.webClient.get().uri("/sboot/vehicle").accept(MediaType.TEXT_PLAIN)
|
|
|
|
this.webClient.get().uri("/sboot/vehicle").accept(MediaType.TEXT_PLAIN)
|
|
|
@ -7492,17 +7484,14 @@ follows:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@DataJpaTest
|
|
|
|
@DataJpaTest
|
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
|
|
|
public class ExampleNonTransactionalTests {
|
|
|
|
class ExampleNonTransactionalTests {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
----
|
|
|
@ -7517,15 +7506,13 @@ shows the `@DataJpaTest` annotation in use:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.*;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.runner.*;
|
|
|
|
|
|
|
|
import org.springframework.boot.test.autoconfigure.orm.jpa.*;
|
|
|
|
import org.springframework.boot.test.autoconfigure.orm.jpa.*;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.*;
|
|
|
|
import static org.assertj.core.api.Assertions.*;
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@DataJpaTest
|
|
|
|
@DataJpaTest
|
|
|
|
public class ExampleRepositoryTests {
|
|
|
|
class ExampleRepositoryTests {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private TestEntityManager entityManager;
|
|
|
|
private TestEntityManager entityManager;
|
|
|
@ -7534,7 +7521,7 @@ shows the `@DataJpaTest` annotation in use:
|
|
|
|
private UserRepository repository;
|
|
|
|
private UserRepository repository;
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testExample() throws Exception {
|
|
|
|
void testExample() throws Exception {
|
|
|
|
this.entityManager.persist(new User("sboot", "1234"));
|
|
|
|
this.entityManager.persist(new User("sboot", "1234"));
|
|
|
|
User user = this.repository.findByUsername("sboot");
|
|
|
|
User user = this.repository.findByUsername("sboot");
|
|
|
|
assertThat(user.getUsername()).isEqualTo("sboot");
|
|
|
|
assertThat(user.getUsername()).isEqualTo("sboot");
|
|
|
@ -7581,17 +7568,14 @@ follows:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@JdbcTest
|
|
|
|
@JdbcTest
|
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
|
|
|
public class ExampleNonTransactionalTests {
|
|
|
|
class ExampleNonTransactionalTests {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
----
|
|
|
@ -7643,14 +7627,11 @@ TIP: A list of the auto-configurations that are enabled by `@JooqTest` can be
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.jooq.DSLContext;
|
|
|
|
import org.jooq.DSLContext;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
import org.springframework.boot.test.autoconfigure.jooq.JooqTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.jooq.JooqTest;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@JooqTest
|
|
|
|
@JooqTest
|
|
|
|
public class ExampleJooqTests {
|
|
|
|
class ExampleJooqTests {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private DSLContext dslContext;
|
|
|
|
private DSLContext dslContext;
|
|
|
@ -7681,13 +7662,10 @@ The following class shows the `@DataMongoTest` annotation in use:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
|
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@DataMongoTest
|
|
|
|
@DataMongoTest
|
|
|
|
public class ExampleDataMongoTests {
|
|
|
|
public class ExampleDataMongoTests {
|
|
|
|
|
|
|
|
|
|
|
@ -7705,12 +7683,9 @@ the following example:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
|
|
|
|
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)
|
|
|
|
@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)
|
|
|
|
public class ExampleDataMongoNonEmbeddedTests {
|
|
|
|
public class ExampleDataMongoNonEmbeddedTests {
|
|
|
|
|
|
|
|
|
|
|
@ -7735,12 +7710,9 @@ The following example shows a typical setup for using Neo4J tests in Spring Boot
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@DataNeo4jTest
|
|
|
|
@DataNeo4jTest
|
|
|
|
public class ExampleDataNeo4jTests {
|
|
|
|
public class ExampleDataNeo4jTests {
|
|
|
|
|
|
|
|
|
|
|
@ -7759,14 +7731,10 @@ as follows:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@DataNeo4jTest
|
|
|
|
@DataNeo4jTest
|
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
|
|
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
|
|
|
public class ExampleNonTransactionalTests {
|
|
|
|
public class ExampleNonTransactionalTests {
|
|
|
@ -7791,12 +7759,9 @@ The following example shows the `@DataRedisTest` annotation in use:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.redis.DataRedisTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.redis.DataRedisTest;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@DataRedisTest
|
|
|
|
@DataRedisTest
|
|
|
|
public class ExampleDataRedisTests {
|
|
|
|
public class ExampleDataRedisTests {
|
|
|
|
|
|
|
|
|
|
|
@ -7824,13 +7789,10 @@ The following example shows the `@DataLdapTest` annotation in use:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest;
|
|
|
|
import org.springframework.ldap.core.LdapTemplate;
|
|
|
|
import org.springframework.ldap.core.LdapTemplate;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@DataLdapTest
|
|
|
|
@DataLdapTest
|
|
|
|
public class ExampleDataLdapTests {
|
|
|
|
public class ExampleDataLdapTests {
|
|
|
|
|
|
|
|
|
|
|
@ -7848,12 +7810,9 @@ following example:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
import org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration;
|
|
|
|
import org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@DataLdapTest(excludeAutoConfiguration = EmbeddedLdapAutoConfiguration.class)
|
|
|
|
@DataLdapTest(excludeAutoConfiguration = EmbeddedLdapAutoConfiguration.class)
|
|
|
|
public class ExampleDataLdapNonEmbeddedTests {
|
|
|
|
public class ExampleDataLdapNonEmbeddedTests {
|
|
|
|
|
|
|
|
|
|
|
@ -7905,7 +7864,7 @@ The specific beans that you want to test should be specified by using the `value
|
|
|
|
==== Auto-configured Spring REST Docs Tests
|
|
|
|
==== Auto-configured Spring REST Docs Tests
|
|
|
|
You can use the `@AutoConfigureRestDocs` annotation to use {spring-rest-docs}[Spring REST
|
|
|
|
You can use the `@AutoConfigureRestDocs` annotation to use {spring-rest-docs}[Spring REST
|
|
|
|
Docs] in your tests with Mock MVC, REST Assured, or WebTestClient. It removes the need for
|
|
|
|
Docs] in your tests with Mock MVC, REST Assured, or WebTestClient. It removes the need for
|
|
|
|
the JUnit rule in Spring REST Docs.
|
|
|
|
the JUnit extension in Spring REST Docs.
|
|
|
|
|
|
|
|
|
|
|
|
`@AutoConfigureRestDocs` can be used to override the default output directory
|
|
|
|
`@AutoConfigureRestDocs` can be used to override the default output directory
|
|
|
|
(`target/generated-snippets` if you are using Maven or `build/generated-snippets` if you
|
|
|
|
(`target/generated-snippets` if you are using Maven or `build/generated-snippets` if you
|
|
|
@ -7922,29 +7881,26 @@ Mock MVC and Spring REST Docs, as shown in the following example:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
|
|
|
|
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
|
|
|
|
@WebMvcTest(UserController.class)
|
|
|
|
@WebMvcTest(UserController.class)
|
|
|
|
@AutoConfigureRestDocs
|
|
|
|
@AutoConfigureRestDocs
|
|
|
|
public class UserDocumentationTests {
|
|
|
|
class UserDocumentationTests {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private MockMvc mvc;
|
|
|
|
private MockMvc mvc;
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void listUsers() throws Exception {
|
|
|
|
void listUsers() throws Exception {
|
|
|
|
this.mvc.perform(get("/users").accept(MediaType.TEXT_PLAIN))
|
|
|
|
this.mvc.perform(get("/users").accept(MediaType.TEXT_PLAIN))
|
|
|
|
.andExpect(status().isOk())
|
|
|
|
.andExpect(status().isOk())
|
|
|
|
.andDo(document("list-users"));
|
|
|
|
.andDo(document("list-users"));
|
|
|
@ -8201,28 +8157,28 @@ which auto-configures one for you.
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-output-capture-test-utility]]
|
|
|
|
[[boot-features-output-capture-test-utility]]
|
|
|
|
==== OutputCapture
|
|
|
|
==== OutputCapture
|
|
|
|
`OutputCapture` is a JUnit `Rule` that you can use to capture `System.out` and
|
|
|
|
`OutputCapture` is a JUnit `Extension` that you can use to capture `System.out` and
|
|
|
|
`System.err` output. You can declare the capture as a `@Rule` and then use `toString()`
|
|
|
|
`System.err` output. You can declare the capture as a `@RegisterExtension` and then use
|
|
|
|
for assertions, as follows:
|
|
|
|
`toString()` for assertions, as follows:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.Rule;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
|
|
|
import org.springframework.boot.test.rule.OutputCapture;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.Matchers.*;
|
|
|
|
import org.springframework.boot.test.extension.OutputCapture;
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class MyTest {
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
@RegisterExtension
|
|
|
|
public OutputCapture capture = new OutputCapture();
|
|
|
|
public OutputCapture output = new OutputCapture();
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void testName() throws Exception {
|
|
|
|
void testName() {
|
|
|
|
System.out.println("Hello World!");
|
|
|
|
System.out.println("Hello World!");
|
|
|
|
assertThat(capture.toString(), containsString("World"));
|
|
|
|
assertThat(this.output.toString()).contains("World"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -8929,16 +8885,15 @@ to limitations in the model kapt provides.
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-kotlin-testing]]
|
|
|
|
[[boot-features-kotlin-testing]]
|
|
|
|
=== Testing
|
|
|
|
=== Testing
|
|
|
|
While it is possible to use JUnit 4 (the default provided by `spring-boot-starter-test`)
|
|
|
|
While it is possible to use JUnit 4 to test Kotlin code, JUnit 5 is recommended. JUnit 5
|
|
|
|
to test Kotlin code, JUnit 5 is recommended. JUnit 5 enables a test class to be
|
|
|
|
enables a test class to be instantiated once and reused for all of the class's tests. This
|
|
|
|
instantiated once and reused for all of the class's tests. This makes it possible to use
|
|
|
|
makes it possible to use `@BeforeClass` and `@AfterClass` annotations on non-static
|
|
|
|
`@BeforeClass` and `@AfterClass` annotations on non-static methods, which is a good fit for
|
|
|
|
methods, which is a good fit for Kotlin.
|
|
|
|
Kotlin.
|
|
|
|
|
|
|
|
|
|
|
|
JUnit 5 is the default and the vintage engine is provided for backward compatibility with
|
|
|
|
To use JUnit 5, exclude `junit:junit` dependency from `spring-boot-starter-test`, add
|
|
|
|
JUnit 4. If you don't use it, exclude `org.junit.vintange:junit-vintage-engine`. See the
|
|
|
|
JUnit 5 dependencies, and configure the Maven or Gradle plugin accordingly. See the
|
|
|
|
{junit5-documentation}/#dependency-metadata-junit-jupiter-samples[JUnit 5 documentation]
|
|
|
|
{junit5-documentation}/#dependency-metadata-junit-jupiter-samples[JUnit 5
|
|
|
|
for more details. You also need to
|
|
|
|
documentation] for more details. You also need to
|
|
|
|
|
|
|
|
{junit5-documentation}/#writing-tests-test-instance-lifecycle-changing-default[switch test
|
|
|
|
{junit5-documentation}/#writing-tests-test-instance-lifecycle-changing-default[switch test
|
|
|
|
instance lifecycle to "per-class"].
|
|
|
|
instance lifecycle to "per-class"].
|
|
|
|
|
|
|
|
|
|
|
|