Commit dea67e9a authored by Stephane Nicoll's avatar Stephane Nicoll

Merge branch '2.0.x'

parents 876def36 af426e38
...@@ -6639,9 +6639,10 @@ providing a `@...Test` annotation that loads the `ApplicationContext` and one or ...@@ -6639,9 +6639,10 @@ providing a `@...Test` annotation that loads the `ApplicationContext` and one or
more `@AutoConfigure...` annotations that can be used to customize auto-configuration more `@AutoConfigure...` annotations that can be used to customize auto-configuration
settings. settings.
NOTE: Each slice loads a very restricted set of auto-configuration classes. If you need NOTE: Each slice restricts component scan to appropriate components and loads a very
to exclude one of them, most `@...Test` annotations provide an `excludeAutoConfiguration` restricted set of auto-configuration classes. If you need to exclude one of them,
attribute. Alternatively, you can use `@ImportAutoConfiguration#exclude`. most `@...Test` annotations provide an `excludeAutoConfiguration` attribute.
Alternatively, you can use `@ImportAutoConfiguration#exclude`.
TIP: It is also possible to use the `@AutoConfigure...` annotations with the standard TIP: It is also possible to use the `@AutoConfigure...` annotations with the standard
`@SpringBootTest` annotation. You can use this combination if you are not interested in `@SpringBootTest` annotation. You can use this combination if you are not interested in
...@@ -6659,10 +6660,13 @@ mapper, which can be one of the following libraries: ...@@ -6659,10 +6660,13 @@ mapper, which can be one of the following libraries:
* `Gson` * `Gson`
* `Jsonb` * `Jsonb`
TIP: A list of the auto-configuration that is enabled by `@JsonTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
If you need to configure elements of the auto-configuration, you can use the If you need to configure elements of the auto-configuration, you can use the
`@AutoConfigureJsonTesters` annotation. `@AutoConfigureJsonTesters` annotation.
Spring Boot includes AssertJ-based helpers that work with the JSONassert and JsonPath Spring Boot includes AssertJ-based helpers that work with the JSONAssert and JsonPath
libraries to check that JSON appears as expected. The `JacksonTester`, `GsonTester`, libraries to check that JSON appears as expected. The `JacksonTester`, `GsonTester`,
`JsonbTester`, and `BasicJsonTester` classes can be used for Jackson, Gson, Jsonb, and `JsonbTester`, and `BasicJsonTester` classes can be used for Jackson, Gson, Jsonb, and
Strings respectively. Any helper fields on the test class can be `@Autowired` when using Strings respectively. Any helper fields on the test class can be `@Autowired` when using
...@@ -6713,9 +6717,6 @@ NOTE: JSON helper classes can also be used directly in standard unit tests. To d ...@@ -6713,9 +6717,6 @@ NOTE: JSON helper classes can also be used directly in standard unit tests. To d
call the `initFields` method of the helper in your `@Before` method if you do not use call the `initFields` method of the helper in your `@Before` method if you do not use
`@JsonTest`. `@JsonTest`.
A list of the auto-configuration that is enabled by `@JsonTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-mvc-tests]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-mvc-tests]]
...@@ -6726,6 +6727,9 @@ scanned beans to `@Controller`, `@ControllerAdvice`, `@JsonComponent`, `Converte ...@@ -6726,6 +6727,9 @@ scanned beans to `@Controller`, `@ControllerAdvice`, `@JsonComponent`, `Converte
`GenericConverter`, `Filter`, `WebMvcConfigurer`, and `HandlerMethodArgumentResolver`. `GenericConverter`, `Filter`, `WebMvcConfigurer`, and `HandlerMethodArgumentResolver`.
Regular `@Component` beans are not scanned when using this annotation. Regular `@Component` beans are not scanned when using this annotation.
TIP: A list of the auto-configuration settings that are enabled by `@WebMvcTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
TIP: If you need to register extra components, such as the Jackson `Module`, you can TIP: If you need to register extra components, such as the Jackson `Module`, you can
import additional configuration classes by using `@Import` on your test. import additional configuration classes by using `@Import` on your test.
...@@ -6819,14 +6823,12 @@ that the driver exits after each test and that a new instance is injected. If yo ...@@ -6819,14 +6823,12 @@ that the driver exits after each test and that a new instance is injected. If yo
not want this behavior, you can add `@Scope("singleton")` to your `WebDriver` `@Bean` not want this behavior, you can add `@Scope("singleton")` to your `WebDriver` `@Bean`
definition. definition.
A list of the auto-configuration settings that are enabled by `@WebMvcTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
TIP: Sometimes writing Spring MVC tests is not enough; Spring Boot can help you run TIP: Sometimes writing Spring MVC tests is not enough; Spring Boot can help you run
<<boot-features-testing-spring-boot-applications-testing-with-running-server, <<boot-features-testing-spring-boot-applications-testing-with-running-server,
full end-to-end tests with an actual server>>. full end-to-end tests with an actual server>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-webflux-tests]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-webflux-tests]]
==== Auto-configured Spring WebFlux Tests ==== Auto-configured Spring WebFlux Tests
To test that {spring-reference}/web-reactive.html[Spring WebFlux] controllers are To test that {spring-reference}/web-reactive.html[Spring WebFlux] controllers are
...@@ -6836,6 +6838,9 @@ auto-configures the Spring WebFlux infrastructure and limits scanned beans to ...@@ -6836,6 +6838,9 @@ auto-configures the Spring WebFlux infrastructure and limits scanned beans to
`WebFluxConfigurer`. Regular `@Component` beans are not scanned when the `@WebFluxTest` `WebFluxConfigurer`. Regular `@Component` beans are not scanned when the `@WebFluxTest`
annotation is used. annotation is used.
TIP: A list of the auto-configuration that is enabled by `@WebFluxTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
TIP: If you need to register extra components, such as Jackson `Module`, you can import TIP: If you need to register extra components, such as Jackson `Module`, you can import
additional configuration classes using `@Import` on your test. additional configuration classes using `@Import` on your test.
...@@ -6888,9 +6893,6 @@ example shows a class that uses both `@WebFluxTest` and a `WebTestClient`: ...@@ -6888,9 +6893,6 @@ example shows a class that uses both `@WebFluxTest` and a `WebTestClient`:
TIP: This setup is only supported by WebFlux applications as using `WebTestClient` in a TIP: This setup is only supported by WebFlux applications as using `WebTestClient` in a
mocked web application only works with WebFlux at the moment. mocked web application only works with WebFlux at the moment.
A list of the auto-configuration that is enabled by `@WebFluxTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
NOTE: `@WebFluxTest` cannot detect routes registered via the functional web framework. For NOTE: `@WebFluxTest` cannot detect routes registered via the functional web framework. For
testing `RouterFunction` beans in the context, consider importing your `RouterFunction` testing `RouterFunction` beans in the context, consider importing your `RouterFunction`
yourself via `@Import` or using `@SpringBootTest`. yourself via `@Import` or using `@SpringBootTest`.
...@@ -6900,6 +6902,7 @@ TIP: Sometimes writing Spring WebFlux tests is not enough; Spring Boot can help ...@@ -6900,6 +6902,7 @@ TIP: Sometimes writing Spring WebFlux tests is not enough; Spring Boot can help
full end-to-end tests with an actual server>>. full end-to-end tests with an actual server>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test]]
==== Auto-configured Data JPA Tests ==== Auto-configured Data JPA Tests
You can use the `@DataJpaTest` annotation to test JPA applications. By default, it You can use the `@DataJpaTest` annotation to test JPA applications. By default, it
...@@ -6907,6 +6910,9 @@ configures an in-memory embedded database, scans for `@Entity` classes, and conf ...@@ -6907,6 +6910,9 @@ configures an in-memory embedded database, scans for `@Entity` classes, and conf
Spring Data JPA repositories. Regular `@Component` beans are not loaded into the Spring Data JPA repositories. Regular `@Component` beans are not loaded into the
`ApplicationContext`. `ApplicationContext`.
TIP: A list of the auto-configuration settings that are enabled by `@DataJpaTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
By default, data JPA tests are transactional and roll back at the end of each test. See By default, data JPA tests are transactional and roll back at the end of each test. See
the {spring-reference}testing.html#testcontext-tx-enabling-transactions[relevant section] the {spring-reference}testing.html#testcontext-tx-enabling-transactions[relevant section]
in the Spring Framework Reference Documentation for more details. If that is not what you in the Spring Framework Reference Documentation for more details. If that is not what you
...@@ -6984,16 +6990,16 @@ following example: ...@@ -6984,16 +6990,16 @@ following example:
} }
---- ----
A list of the auto-configuration settings that are enabled by `@DataJpaTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-jdbc-test]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-jdbc-test]]
==== Auto-configured JDBC Tests ==== Auto-configured JDBC Tests
`@JdbcTest` is similar to `@DataJpaTest` but is for pure JDBC-related tests. By default, `@JdbcTest` is similar to `@DataJpaTest` but is for tests that only require a
it also configures an in-memory embedded database and a `JdbcTemplate`. Regular `DataSource`. By default, it configures an in-memory embedded database and a
`@Component` beans are not loaded into the `ApplicationContext`. `JdbcTemplate`. Regular `@Component` beans are not loaded into the `ApplicationContext`.
TIP: A list of the auto-configuration that is enabled by `@JdbcTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
By default, JDBC tests are transactional and roll back at the end of each test. See the By default, JDBC tests are transactional and roll back at the end of each test. See the
{spring-reference}testing.html#testcontext-tx-enabling-transactions[relevant section] in {spring-reference}testing.html#testcontext-tx-enabling-transactions[relevant section] in
...@@ -7022,9 +7028,6 @@ If you prefer your test to run against a real database, you can use the ...@@ -7022,9 +7028,6 @@ If you prefer your test to run against a real database, you can use the
`@AutoConfigureTestDatabase` annotation in the same way as for `DataJpaTest`. (See `@AutoConfigureTestDatabase` annotation in the same way as for `DataJpaTest`. (See
"<<boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test>>".) "<<boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test>>".)
A list of the auto-configuration that is enabled by `@JdbcTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-jooq-test]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-jooq-test]]
...@@ -7033,7 +7036,11 @@ You can use `@JooqTest` in a similar fashion as `@JdbcTest` but for jOOQ-related ...@@ -7033,7 +7036,11 @@ You can use `@JooqTest` in a similar fashion as `@JdbcTest` but for jOOQ-related
As jOOQ relies heavily on a Java-based schema that corresponds with the database schema, As jOOQ relies heavily on a Java-based schema that corresponds with the database schema,
the existing `DataSource` is used. If you want to replace it with an in-memory database, the existing `DataSource` is used. If you want to replace it with an in-memory database,
you can use `@AutoConfigureTestDatabase` to override those settings. (For more about using you can use `@AutoConfigureTestDatabase` to override those settings. (For more about using
jOOQ with Spring Boot, see "<<boot-features-jooq>>", earlier in this chapter.) jOOQ with Spring Boot, see "<<boot-features-jooq>>", earlier in this chapter.) Regular
`@Component` beans are not loaded into the `ApplicationContext`.
TIP: A list of the auto-configuration that is enabled by `@JooqTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
`@JooqTest` configures a `DSLContext`. Regular `@Component` beans are not loaded into the `@JooqTest` configures a `DSLContext`. Regular `@Component` beans are not loaded into the
`ApplicationContext`. The following example shows the `@JooqTest` annotation in use: `ApplicationContext`. The following example shows the `@JooqTest` annotation in use:
...@@ -7061,8 +7068,6 @@ test class as ...@@ -7061,8 +7068,6 @@ test class as
<<boot-features-testing-spring-boot-applications-testing-autoconfigured-jdbc-test,shown <<boot-features-testing-spring-boot-applications-testing-autoconfigured-jdbc-test,shown
in the JDBC example>>. in the JDBC example>>.
A list of the auto-configuration that is enabled by `@JooqTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
...@@ -7074,6 +7079,9 @@ in-memory embedded MongoDB (if available), configures a `MongoTemplate`, scans f ...@@ -7074,6 +7079,9 @@ in-memory embedded MongoDB (if available), configures a `MongoTemplate`, scans f
`@Component` beans are not loaded into the `ApplicationContext`. (For more about using `@Component` beans are not loaded into the `ApplicationContext`. (For more about using
MongoDB with Spring Boot, see "<<boot-features-mongodb>>", earlier in this chapter.) MongoDB with Spring Boot, see "<<boot-features-mongodb>>", earlier in this chapter.)
TIP: A list of the auto-configuration settings that are enabled by `@DataMongoTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
The following class shows the `@DataMongoTest` annotation in use: The following class shows the `@DataMongoTest` annotation in use:
[source,java,indent=0] [source,java,indent=0]
...@@ -7114,8 +7122,6 @@ the following example: ...@@ -7114,8 +7122,6 @@ the following example:
} }
---- ----
A list of the auto-configuration settings that are enabled by `@DataMongoTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
...@@ -7127,6 +7133,9 @@ configures Spring Data Neo4j repositories. Regular `@Component` beans are not lo ...@@ -7127,6 +7133,9 @@ configures Spring Data Neo4j repositories. Regular `@Component` beans are not lo
the `ApplicationContext`. (For more about using Neo4J with Spring Boot, see the `ApplicationContext`. (For more about using Neo4J with Spring Boot, see
"<<boot-features-neo4j>>", earlier in this chapter.) "<<boot-features-neo4j>>", earlier in this chapter.)
TIP: A list of the auto-configuration settings that are enabled by `@DataNeo4jTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
The following example shows a typical setup for using Neo4J tests in Spring Boot: The following example shows a typical setup for using Neo4J tests in Spring Boot:
[source,java,indent=0] [source,java,indent=0]
...@@ -7171,9 +7180,6 @@ as follows: ...@@ -7171,9 +7180,6 @@ as follows:
---- ----
A list of the auto-configuration settings that are enabled by `@DataNeo4jTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-redis-test]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-redis-test]]
...@@ -7183,6 +7189,9 @@ You can use `@DataRedisTest` to test Redis applications. By default, it scans fo ...@@ -7183,6 +7189,9 @@ You can use `@DataRedisTest` to test Redis applications. By default, it scans fo
beans are not loaded into the `ApplicationContext`. (For more about using Redis with beans are not loaded into the `ApplicationContext`. (For more about using Redis with
Spring Boot, see "<<boot-features-redis>>", earlier in this chapter.) Spring Boot, see "<<boot-features-redis>>", earlier in this chapter.)
TIP: A list of the auto-configuration settings that are enabled by `@DataRedisTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
The following example shows the `@DataRedisTest` annotation in use: The following example shows the `@DataRedisTest` annotation in use:
[source,java,indent=0] [source,java,indent=0]
...@@ -7203,9 +7212,6 @@ The following example shows the `@DataRedisTest` annotation in use: ...@@ -7203,9 +7212,6 @@ The following example shows the `@DataRedisTest` annotation in use:
} }
---- ----
A list of the auto-configuration settings that are enabled by `@DataRedisTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-ldap-test]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-ldap-test]]
...@@ -7216,6 +7222,9 @@ classes, and configures Spring Data LDAP repositories. Regular `@Component` bean ...@@ -7216,6 +7222,9 @@ classes, and configures Spring Data LDAP repositories. Regular `@Component` bean
loaded into the `ApplicationContext`. (For more about using LDAP with loaded into the `ApplicationContext`. (For more about using LDAP with
Spring Boot, see "<<boot-features-ldap>>", earlier in this chapter.) Spring Boot, see "<<boot-features-ldap>>", earlier in this chapter.)
TIP: A list of the auto-configuration settings that are enabled by `@DataLdapTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
The following example shows the `@DataLdapTest` annotation in use: The following example shows the `@DataLdapTest` annotation in use:
[source,java,indent=0] [source,java,indent=0]
...@@ -7256,19 +7265,20 @@ following example: ...@@ -7256,19 +7265,20 @@ following example:
} }
---- ----
A list of the auto-configuration settings that are enabled by `@DataLdapTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-client]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-client]]
==== Auto-configured REST Clients ==== Auto-configured REST Clients
You can use the `@RestClientTest` annotation to test REST clients. By default, it You can use the `@RestClientTest` annotation to test REST clients. By default, it
auto-configures Jackson, GSON, and Jsonb support, configures a `RestTemplateBuilder`, and auto-configures Jackson, GSON, and Jsonb support, configures a `RestTemplateBuilder`, and
adds support for `MockRestServiceServer`. The specific beans that you want to test should adds support for `MockRestServiceServer`. Regular `@Component` beans are not loaded into
be specified by using the `value` or `components` attribute of `@RestClientTest`, as the `ApplicationContext`.
shown in the following example:
TIP: A list of the auto-configuration settings that are enabled by `@RestClientTest` can
be <<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
The specific beans that you want to test should be specified by using the `value` or
`components` attribute of `@RestClientTest`, as shown in the following example:
[source,java,indent=0] [source,java,indent=0]
---- ----
...@@ -7294,9 +7304,6 @@ shown in the following example: ...@@ -7294,9 +7304,6 @@ shown in the following example:
} }
---- ----
A list of the auto-configuration settings that are enabled by `@RestClientTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs]]
...@@ -7310,9 +7317,10 @@ in Spring REST Docs. ...@@ -7310,9 +7317,10 @@ in Spring REST Docs.
are using Gradle). It can also be used to configure the host, scheme, and port that are using Gradle). It can also be used to configure the host, scheme, and port that
appears in any documented URIs. appears in any documented URIs.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-mock-mvc]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-mock-mvc]]
===== Auto-configured Spring REST Docs Tests with Mock MVC ===== Auto-configured Spring REST Docs Tests with Mock MVC
`@AutoConfigureRestDocs` customizes the `MockMvc` bean to use Spring REST Docs. You can `@AutoConfigureRestDocs` customizes the `MockMvc` bean to use Spring REST Docs. You can
inject it by using `@Autowired` and use it in your tests as you normally would when using inject it by using `@Autowired` and use it in your tests as you normally would when using
Mock MVC and Spring REST Docs, as shown in the following example: Mock MVC and Spring REST Docs, as shown in the following example:
...@@ -7391,7 +7399,6 @@ generate the default snippets. The following example shows a ...@@ -7391,7 +7399,6 @@ generate the default snippets. The following example shows a
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-rest-assured]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-rest-assured]]
===== Auto-configured Spring REST Docs Tests with REST Assured ===== Auto-configured Spring REST Docs Tests with REST Assured
`@AutoConfigureRestDocs` makes a `RequestSpecification` bean, preconfigured to use Spring `@AutoConfigureRestDocs` makes a `RequestSpecification` bean, preconfigured to use Spring
REST Docs, available to your tests. You can inject it by using `@Autowired` and use it in REST Docs, available to your tests. You can inject it by using `@Autowired` and use it in
your tests as you normally would when using REST Assured and Spring REST Docs, as shown your tests as you normally would when using REST Assured and Spring REST Docs, as shown
...@@ -7413,6 +7420,28 @@ include::{code-examples}/test/autoconfigure/restdocs/restassured/AdvancedConfigu ...@@ -7413,6 +7420,28 @@ include::{code-examples}/test/autoconfigure/restdocs/restassured/AdvancedConfigu
[[boot-features-testing-spring-boot-applications-testing-auto-configured-additional-auto-config]]
==== Additional Auto-configuration and Slicing
Each slice provides one or more `@AutoConfigure...` annotations that namely defines the
auto-configurations that should be included as part of a slice. Additional
auto-configurations can be added by creating a custom `@AutoConfigure` annotation or
simply by adding `@ImportAutoConfiguration` to the test as shown in the following example:
[source,java,indent=0]
----
@RunWith(SpringRunner.class)
@JdbcTest
@ImportAutoConfiguration(IntegrationAutoConfiguration.class)
public class ExampleJdbcTests {
}
----
NOTE: Make sure to not use the regular `@Import` annotation to import auto-configurations
as they are handled in a specific way by Spring Boot.
[[boot-features-testing-spring-boot-applications-testing-user-configuration]] [[boot-features-testing-spring-boot-applications-testing-user-configuration]]
==== User Configuration and Slicing ==== User Configuration and Slicing
If you <<using-boot-structuring-your-code, structure your code>> in a sensible way, your If you <<using-boot-structuring-your-code, structure your code>> in a sensible way, your
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment