[[howto.testing]]
= Testing

Spring Boot includes a number of testing utilities and support classes as well as a dedicated starter that provides common test dependencies.
This section answers common questions about testing.



[[howto.testing.with-spring-security]]
== Testing With Spring Security

Spring Security provides support for running tests as a specific user.
For example, the test in the snippet below will run with an authenticated user that has the `ADMIN` role.

include-code::MySecurityTests[]

Spring Security provides comprehensive integration with Spring MVC Test, and this can also be used when testing controllers using the javadoc:org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest[format=annotation] slice and javadoc:org.springframework.test.web.servlet.MockMvc[].

For additional details on Spring Security's testing support, see Spring Security's {url-spring-security-docs}/servlet/test/index.html[reference documentation].




[[howto.testing.slice-tests]]
== Structure javadoc:org.springframework.context.annotation.Configuration[format=annotation] Classes for Inclusion in Slice Tests

Slice tests work by restricting Spring Framework's component scanning to a limited set of components based on their type.
For any beans that are not created through component scanning, for example, beans that are created using the javadoc:org.springframework.context.annotation.Bean[format=annotation] annotation, slice tests will not be able to include/exclude them from the application context.
Consider this example:

include-code::MyConfiguration[]

For a javadoc:org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest[format=annotation] for an application with the above javadoc:org.springframework.context.annotation.Configuration[format=annotation] class, you might expect to have the javadoc:org.springframework.security.web.SecurityFilterChain[] bean in the application context so that you can test if your controller endpoints are secured properly.
However, `MyConfiguration` is not picked up by @WebMvcTest's component scanning filter because it doesn't match any of the types specified by the filter.
You can include the configuration explicitly by annotating the test class with `@Import(MyConfiguration.class)`.
This will load all the beans in `MyConfiguration` including the javadoc:com.zaxxer.hikari.HikariDataSource[] bean which isn't required when testing the web tier.
Splitting the configuration class into two will enable importing just the security configuration.

include-code::MySecurityConfiguration[]

include-code::MyDatasourceConfiguration[]

Having a single configuration class can be inefficient when beans from a certain domain need to be included in slice tests.
Instead, structuring the application's configuration as multiple granular classes with beans for a specific domain can enable importing them only for specific slice tests.



[[howto.testing.testcontainers-api-downgrade]]
==  Downgrading Testcontainers Docker API Version

Recent versions of Docker Desktop do not support the default API version used by the Testcontainers dependency managed by Spring Boot.
To help prevent issues, Spring Boot ships with `docker-java.properties` files in the `spring-boot-testcontainers` and `spring-boot-test` jars that upgrade the API version that Testcontainers uses.

If you don't want the API version to be changed, you should add a `docker-java.properties` file in your own projects classpath.
An empty file will restore default behavior, or you can set a `api.version` property to whatever value you prefer.
