How to resolve IllegalStateException Unable to find @SpringBootConfiguration exception
1. The exception
When testing a Spring Boot application, you might encounter the IllegalStateException: Unable to find a @SpringBootConfiguration
exception, as shown below:
2. The project layout
The project layout is as follows:
The Application.java
code is:
The test code is:
- The
@SpringBootApplication
is inApplication.java
, which has a package namedcom.example1
. - The test class is in the
com.example2
package, which is neither the same as the@SpringBootApplication
package nor a subpackage of it.
This image illustrates the package difference:
3. The reason for the Unable to find @SpringBootConfiguration
exception
The root cause of the Unable to find @SpringBootConfiguration
exception is that the test class cannot locate the @SpringBootApplication
class.
But why does this happen? Even though the @SpringBootApplication
class exists, the test class cannot “see” it because of the specific mechanism used by @SpringBootTest
to locate the @SpringBootApplication
class. The mechanism works as follows:
As shown in the image, the @SpringBootTest
class searches for the @SpringBootApplication
class starting from its own package (com.example2
) and moves up to the root package (/
). If it fails to find the @SpringBootApplication
class, it throws the Unable to find @SpringBootConfiguration
exception.
Here is the official description of this mechanism:
The search algorithm works up from the package that contains the test until it finds a
@SpringBootApplication
or@SpringBootConfiguration
annotated class. As long as you’ve structured your code in a sensible way, your main configuration is usually found.
4. How to resolve this problem
There are three ways to resolve this exception:
- Move your test class to the
com.example1
package, which is the same as the@SpringBootApplication
class. - Move your test class to a subpackage of
com.example1
, for example,com.example1.test
. - Use
@SpringBootTest(classes = Application.class)
, whereApplication.class
is the@SpringBootApplication
class.
4.1 Way 1: Move the test class to the same package
4.2 Way 2: Move the test class to a subpackage
4.3 Way 3: Specify the @SpringBootApplication
class explicitly
Modify the @SpringBootTest
class as follows:
Key changes:
- Import the
com.example1.Application
class. - Add the
classes
parameter to the@SpringBootTest
annotation to specify the@SpringBootApplication
class explicitly.
After making these changes, rerun the test, and the exception should disappear.
5. Summary
The IllegalStateException: Unable to find @SpringBootConfiguration
exception occurs when the test class cannot locate the @SpringBootApplication
class due to package misalignment. To resolve this issue, ensure your test class is in the same package or a subpackage of the @SpringBootApplication
class. Alternatively, you can explicitly specify the @SpringBootApplication
class in the @SpringBootTest
annotation. Proper package structure and configuration are key to avoiding this exception.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
- 👨💻 springboot
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!