Exception is raised if the test is not located in the same or sub package of @SpringBootApplication main class, even if I use @ContextConfiguration or @SpringBootTest(classes=...) according to the suggestion in exception message.
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
at org.springframework.util.Assert.state(Assert.java:76)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.findConfigurationClass(SpringBootTestContextBootstrapper.java:246)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:233)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:150)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:351)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:267)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:215)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:108)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:111)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:142)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:126)
at org.springframework.test.context.junit.jupiter.SpringExtension.getTestContextManager(SpringExtension.java:366)
at org.springframework.test.context.junit.jupiter.SpringExtension.beforeAll(SpringExtension.java:131)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
the reproducer project configuration-class-not-found.zip
Comment From: wilkinsona
This is the expected behavior. In both SpringBootTestWithContextConfigurationTests and SpringBootTestWithClassesAttributeTests the nested Config class is annotated with @TestConfiguration. @TestConfiguration is specifically intended for use where you want the configuration to be used in addition to the auto-detection of @SpringBootConfiguration:
If you want to customize the primary configuration, you can use a nested
@TestConfigurationclass. Unlike a nested@Configurationclass, which would be used instead of your application’s primary configuration, a nested@TestConfigurationclass is used in addition to your application’s primary configuration.
If you use @Configuration instead of @TestConfiguration the tests both pass.
Comment From: philwebb
Reopening due to https://github.com/spring-projects/spring-boot/pull/43358#issuecomment-2515959802