A configuration class declares application configuration, including beans that Spring Boot adds to an application context. Component scanning discovers configuration classes on the classpath when they reside in the main application package or a subpackage. section 3.3.3
Choose the configuration form according to its scope:
- Place a regular @Configuration class under the main application package to make its beans available broadly, including to every matching @SpringBootTest. section 3.3.3
- Use @TestConfiguration for test-only beans that should be added explicitly, either by nesting it in a test or importing it with @Import. section 3.3.3
- Avoid nesting ordinary @Configuration in a test: it can replace the application’s entire configuration instead of adding selected beans. section 3.3.3
Adding a test bean selectively
@SpringBootTest @Import(CustomTestConfiguration.class) class ImportConfigTests { } A nested @TestConfiguration can instead declare @Bean methods directly, such as myCustomThing(), and applies only to the test that includes it.


