Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to find a @SpringBootConfiguration results in misleading error message #43357

Closed
quaff opened this issue Dec 3, 2024 · 2 comments
Closed
Assignees
Labels
type: bug A general bug
Milestone

Comments

@quaff
Copy link
Contributor

quaff commented Dec 3, 2024

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

@wilkinsona
Copy link
Member

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 @TestConfiguration class. Unlike a nested @Configuration class, which would be used instead of your application’s primary configuration, a nested @TestConfiguration class is used in addition to your application’s primary configuration.

If you use @Configuration instead of @TestConfiguration the tests both pass.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale Dec 3, 2024
@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 3, 2024
@philwebb philwebb changed the title Unable to find a @SpringBootConfiguration Unable to find a @SpringBootConfiguration results in somewhat misleading error message Dec 4, 2024
@philwebb philwebb reopened this Dec 4, 2024
@philwebb philwebb added type: bug A general bug and removed status: invalid An issue that we don't feel is valid labels Dec 4, 2024
@philwebb philwebb added this to the 3.3.x milestone Dec 4, 2024
@philwebb philwebb added the status: pending-design-work Needs design work before any code can be developed label Dec 4, 2024
@philwebb
Copy link
Member

philwebb commented Dec 4, 2024

Reopening due to #43358 (comment)

@philwebb philwebb changed the title Unable to find a @SpringBootConfiguration results in somewhat misleading error message Unable to find a @SpringBootConfiguration results in misleading error message Dec 14, 2024
@philwebb philwebb self-assigned this Dec 14, 2024
@philwebb philwebb removed the status: pending-design-work Needs design work before any code can be developed label Dec 14, 2024
@philwebb philwebb modified the milestones: 3.3.x, 3.3.7 Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants