How @SpringBootTest Finds the Main Class
Fast recall
Quick revision
Core answer
@SpringBootTest(classes = MyApp.class) explicitly.Understanding of Spring Boot's component scanning and test context discovery mechanism
Putting test classes in a different package tree than the main class and getting NoSuchBeanDefinitionException
Mention you can explicitly specify the config class with @SpringBootTest(classes = MyApp.class) or use @ContextConfiguration
Interview-ready guide
Interview answer
When you use the SpringBootTest annotation, Spring Boot needs to find your main application class — the one with the SpringBootApplication annotation — so it knows how to set up the context.
It does this by starting at the package where your test class is and searching upward through the parent packages. So if your test is in com dot company dot myapp dot service, Spring Boot checks that package, then com dot company dot myapp, and so on, until it finds a class with the SpringBootApplication annotation.
This is why the standard project structure works automatically — you put your main class at the root package like com dot company dot myapp, and your tests in the same package or sub-packages, and everything just works. The common mistake freshers make is organising tests by type rather than by component.
Like putting all integration tests in com dot company dot integration tests while the main class is in com dot company dot myapp. Now Spring Boot can't find the main class because it's in a completely different package tree, and you get an error about being unable to find a Spring Boot configuration.
The fix is either to restructure your packages, or to explicitly tell Spring Boot which class to use with the classes attribute on the SpringBootTest annotation. That overrides the automatic discovery and points directly to your main class.
How @SpringBootTest Finds the Main Class
Direct Answer
When no classes are supplied, Spring Boot searches upward from the test package for a class annotated with @SpringBootConfiguration; @SpringBootApplication includes that annotation. Keeping tests in the application package tree lets discovery work. Otherwise, set @SpringBootTest(classes = MyApp.class) explicitly.
Code Example
// Main application class at the root package
package com.example.orderservice;
@SpringBootApplication
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
// Test in the same package tree — Spring Boot finds the main class automatically
package com.example.orderservice.controller;
@SpringBootTest
class OrderControllerTest {
// scans up from com.example.orderservice.controller
// finds OrderServiceApplication at com.example.orderservice
}
// Test in a different tree — name the main class explicitly
package com.different.packagetree;
@SpringBootTest(classes = OrderServiceApplication.class) // explicit config
class ExternalIntegrationTest {
// now Spring Boot knows exactly which application class to use
}
Common Mistakes & Pitfalls
- Test class in unrelated package: Freshers sometimes organize tests by test type rather than by component, putting all integration tests at com.company.integrationtests while the main class is at com.company.myapp — Spring Boot can't find the main class and the test fails with "Unable to find a @SpringBootConfiguration".
- Renaming the main class package: If you refactor your main class from com.oldname to com.newname but forget to update test package names, old tests suddenly can't find the application class.
- Creating nested configuration in tests: Some freshers create a @SpringBootConfiguration class inside their test class, which overrides the main application config and loads only that, missing all the real beans.
Check your understanding
Answer these without looking back at the article.
- What error do you get when @SpringBootTest can't find the application class?
- Can you use @SpringBootTest with a custom @Configuration class instead of the main app?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.