|
|
|
|
@@ -35,7 +35,7 @@ To use the vintage engine, add a dependency on `junit-vintage-engine`, as shown
|
|
|
|
|
The `spring-boot-starter-test` "`Starter`" (in the `test` `scope`) contains the following provided libraries:
|
|
|
|
|
|
|
|
|
|
* https://junit.org/junit5/[JUnit 5]: The de-facto standard for unit testing Java applications.
|
|
|
|
|
* {spring-framework-docs}/testing.html#integration-testing[Spring Test] & Spring Boot Test: Utilities and integration test support for Spring Boot applications.
|
|
|
|
|
* {spring-framework-docs}/testing/integration.html[Spring Test] & Spring Boot Test: Utilities and integration test support for Spring Boot applications.
|
|
|
|
|
* https://assertj.github.io/doc/[AssertJ]: A fluent assertion library.
|
|
|
|
|
* https://github.com/hamcrest/JavaHamcrest[Hamcrest]: A library of matcher objects (also known as constraints or predicates).
|
|
|
|
|
* https://site.mockito.org/[Mockito]: A Java mocking framework.
|
|
|
|
|
@@ -59,7 +59,7 @@ It is useful to be able to perform integration testing without requiring deploym
|
|
|
|
|
The Spring Framework includes a dedicated test module for such integration testing.
|
|
|
|
|
You can declare a dependency directly to `org.springframework:spring-test` or use the `spring-boot-starter-test` "`Starter`" to pull it in transitively.
|
|
|
|
|
|
|
|
|
|
If you have not used the `spring-test` module before, you should start by reading the {spring-framework-docs}/testing.html#testing[relevant section] of the Spring Framework reference documentation.
|
|
|
|
|
If you have not used the `spring-test` module before, you should start by reading the {spring-framework-docs}/testing.html[relevant section] of the Spring Framework reference documentation.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -198,13 +198,13 @@ include::code:MyApplicationArgumentTests[]
|
|
|
|
|
==== Testing With a Mock Environment
|
|
|
|
|
By default, `@SpringBootTest` does not start the server but instead sets up a mock environment for testing web endpoints.
|
|
|
|
|
|
|
|
|
|
With Spring MVC, we can query our web endpoints using {spring-framework-docs}/testing.html#spring-mvc-test-framework[`MockMvc`] or `WebTestClient`, as shown in the following example:
|
|
|
|
|
With Spring MVC, we can query our web endpoints using {spring-framework-docs}/testing/spring-mvc-test-framework.html[`MockMvc`] or `WebTestClient`, as shown in the following example:
|
|
|
|
|
|
|
|
|
|
include::code:MyMockMvcTests[]
|
|
|
|
|
|
|
|
|
|
TIP: If you want to focus only on the web layer and not start a complete `ApplicationContext`, consider <<features#features.testing.spring-boot-applications.spring-mvc-tests,using `@WebMvcTest` instead>>.
|
|
|
|
|
|
|
|
|
|
With Spring WebFlux endpoints, you can use {spring-framework-docs}/testing.html#webtestclient-tests[`WebTestClient`] as shown in the following example:
|
|
|
|
|
With Spring WebFlux endpoints, you can use {spring-framework-docs}/testing/webtestclient.html[`WebTestClient`] as shown in the following example:
|
|
|
|
|
|
|
|
|
|
include::code:MyMockWebTestClientTests[]
|
|
|
|
|
|
|
|
|
|
@@ -226,7 +226,7 @@ If you need to start a full running server, we recommend that you use random por
|
|
|
|
|
If you use `@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)`, an available port is picked at random each time your test runs.
|
|
|
|
|
|
|
|
|
|
The `@LocalServerPort` annotation can be used to <<howto#howto.webserver.discover-port,inject the actual port used>> into your test.
|
|
|
|
|
For convenience, tests that need to make REST calls to the started server can additionally `@Autowire` a {spring-framework-docs}/testing.html#webtestclient-tests[`WebTestClient`], which resolves relative links to the running server and comes with a dedicated API for verifying responses, as shown in the following example:
|
|
|
|
|
For convenience, tests that need to make REST calls to the started server can additionally `@Autowire` a {spring-framework-docs}/testing/webtestclient.html[`WebTestClient`], which resolves relative links to the running server and comes with a dedicated API for verifying responses, as shown in the following example:
|
|
|
|
|
|
|
|
|
|
include::code:MyRandomPortWebTestClientTests[]
|
|
|
|
|
|
|
|
|
|
@@ -419,7 +419,7 @@ TIP: If you need to register extra components, such as Jackson `Module`, you can
|
|
|
|
|
|
|
|
|
|
Often, `@WebFluxTest` is limited to a single controller and used in combination with the `@MockBean` annotation to provide mock implementations for required collaborators.
|
|
|
|
|
|
|
|
|
|
`@WebFluxTest` also auto-configures {spring-framework-docs}/testing.html#webtestclient[`WebTestClient`], which offers a powerful way to quickly test WebFlux controllers without needing to start a full HTTP server.
|
|
|
|
|
`@WebFluxTest` also auto-configures {spring-framework-docs}/testing/webtestclient.html[`WebTestClient`], which offers a powerful way to quickly test WebFlux controllers without needing to start a full HTTP server.
|
|
|
|
|
|
|
|
|
|
TIP: You can also auto-configure `WebTestClient` in a non-`@WebFluxTest` (such as `@SpringBootTest`) by annotating it with `@AutoConfigureWebTestClient`.
|
|
|
|
|
The following example shows a class that uses both `@WebFluxTest` and a `WebTestClient`:
|
|
|
|
|
@@ -559,7 +559,7 @@ Regular `@Component` and `@ConfigurationProperties` beans are not scanned when t
|
|
|
|
|
TIP: A list of the auto-configuration settings that are enabled by `@DataJpaTest` can be <<test-auto-configuration#appendix.test-auto-configuration,found in the appendix>>.
|
|
|
|
|
|
|
|
|
|
By default, data JPA tests are transactional and roll back at the end of each test.
|
|
|
|
|
See the {spring-framework-docs}/testing.html#testcontext-tx-enabling-transactions[relevant section] in the Spring Framework Reference Documentation for more details.
|
|
|
|
|
See the {spring-framework-docs}/testing/testcontext-framework/tx.html#testcontext-tx-enabling-transactions[relevant section] in the Spring Framework Reference Documentation for more details.
|
|
|
|
|
If that is not what you want, you can disable transaction management for a test or for the whole class as follows:
|
|
|
|
|
|
|
|
|
|
include::code:MyNonTransactionalTests[]
|
|
|
|
|
@@ -591,7 +591,7 @@ Regular `@Component` and `@ConfigurationProperties` beans are not scanned when t
|
|
|
|
|
TIP: A list of the auto-configurations that are enabled by `@JdbcTest` can be <<test-auto-configuration#appendix.test-auto-configuration,found in the appendix>>.
|
|
|
|
|
|
|
|
|
|
By default, JDBC tests are transactional and roll back at the end of each test.
|
|
|
|
|
See the {spring-framework-docs}/testing.html#testcontext-tx-enabling-transactions[relevant section] in the Spring Framework Reference Documentation for more details.
|
|
|
|
|
See the {spring-framework-docs}/testing/testcontext-framework/tx.html#testcontext-tx-enabling-transactions[relevant section] in the Spring Framework Reference Documentation for more details.
|
|
|
|
|
If that is not what you want, you can disable transaction management for a test or for the whole class, as follows:
|
|
|
|
|
|
|
|
|
|
include::code:MyTransactionalTests[]
|
|
|
|
|
@@ -611,7 +611,7 @@ Only `AbstractJdbcConfiguration` subclasses are scanned when the `@DataJdbcTest`
|
|
|
|
|
TIP: A list of the auto-configurations that are enabled by `@DataJdbcTest` can be <<test-auto-configuration#appendix.test-auto-configuration,found in the appendix>>.
|
|
|
|
|
|
|
|
|
|
By default, Data JDBC tests are transactional and roll back at the end of each test.
|
|
|
|
|
See the {spring-framework-docs}/testing.html#testcontext-tx-enabling-transactions[relevant section] in the Spring Framework Reference Documentation for more details.
|
|
|
|
|
See the {spring-framework-docs}/testing/testcontext-framework/tx.html#testcontext-tx-enabling-transactions[relevant section] in the Spring Framework Reference Documentation for more details.
|
|
|
|
|
If that is not what you want, you can disable transaction management for a test or for the whole test class as <<features#features.testing.spring-boot-applications.autoconfigured-jdbc,shown in the JDBC example>>.
|
|
|
|
|
|
|
|
|
|
If you prefer your test to run against a real database, you can use the `@AutoConfigureTestDatabase` annotation in the same way as for `@DataJpaTest`.
|
|
|
|
|
@@ -687,7 +687,7 @@ The following example shows a typical setup for using Neo4J tests in Spring Boot
|
|
|
|
|
include::code:propagation/MyDataNeo4jTests[]
|
|
|
|
|
|
|
|
|
|
By default, Data Neo4j tests are transactional and roll back at the end of each test.
|
|
|
|
|
See the {spring-framework-docs}/testing.html#testcontext-tx-enabling-transactions[relevant section] in the Spring Framework Reference Documentation for more details.
|
|
|
|
|
See the {spring-framework-docs}/testing/testcontext-framework/tx.html#testcontext-tx-enabling-transactions[relevant section] in the Spring Framework Reference Documentation for more details.
|
|
|
|
|
If that is not what you want, you can disable transaction management for a test or for the whole class, as follows:
|
|
|
|
|
|
|
|
|
|
include::code:nopropagation/MyDataNeo4jTests[]
|
|
|
|
|
|