Spring Boot
Component Scan
Base Package
Configuration
Testing

Configuring base package for component scan in Spring boot test

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Spring Boot tests, component scanning usually starts from the package of the @SpringBootApplication class. If you need a different scan scope in a test, the right solution is often not to widen scanning globally, but to load a smaller, explicit test configuration that imports only the beans the test actually needs.

Core Sections

Understand the default behavior first

Spring Boot normally finds the main application configuration and scans from that package downward. That is why the standard advice is to place the application class near the root of the codebase.

For many integration tests, this is enough:

java
@SpringBootTest
class OrderServiceTest {
}

If the test class lives in a package that still allows Boot to find the main application class, you usually do not need a special @ComponentScan at all.

Use a dedicated test configuration when you need a custom scan scope

If the test should load only a narrow part of the application, declare a test configuration class and control scanning there.

java
1@SpringBootTest(classes = TestConfig.class)
2class BillingServiceTest {
3}
4
5@Configuration
6@ComponentScan(basePackages = "com.example.billing")
7class TestConfig {
8}

This is clearer than trying to bend the main application scan rules for one test case.

Prefer @Import for a few known beans

If the test needs only a handful of components, @Import is often better than package scanning because it is explicit and fast.

java
1@SpringBootTest(classes = TestConfig.class)
2class PricingServiceTest {
3}
4
5@Configuration
6@Import({PricingService.class, TaxCalculator.class})
7class TestConfig {
8}

This approach avoids accidentally pulling in unrelated beans, repositories, or controllers just because they share a package prefix. That usually improves startup time as well, which matters when the same test class runs repeatedly in CI or when a suite contains many context-loading tests.

Use slice tests when possible

Sometimes the real fix is not custom component scanning but using a narrower Spring test annotation. For example:

  • '@WebMvcTest for MVC controllers'
  • '@DataJpaTest for JPA repositories'
  • '@JsonTest for JSON configuration'

These annotations already limit what gets loaded, which is usually better than manually fighting @SpringBootTest into acting like a slice test.

Be careful with package layout and test location

If Spring Boot cannot find the main application configuration at all, developers sometimes try to compensate with @ComponentScan on the test. That can work, but it is often masking a package-layout issue.

A cleaner fix may be:

  • move the test into a package under the application root
  • point @SpringBootTest(classes = MyApplication.class) at the real boot class
  • use explicit test config only for custom narrowing

That keeps the scanning rules closer to how the app behaves in production. It also prevents tests from passing only because they load a bean graph that the real application startup path would never create.

Common Pitfalls

  • Adding broad @ComponentScan rules to tests when @SpringBootTest(classes = MyApplication.class) would already solve the problem cleanly.
  • Using package scanning in tests when @Import would be clearer and would load fewer beans.
  • Trying to turn a full integration test into a slice test manually instead of using the Spring test slice annotations designed for that purpose.
  • Masking a package-structure problem by piling on scan annotations rather than fixing where the test or application class lives.
  • Loading extra beans unintentionally because the configured base package is wider than the test really needs.

Summary

  • Spring Boot tests usually inherit component scanning from the main application configuration.
  • For custom scan scope, prefer a dedicated test configuration class.
  • If only a few beans are needed, @Import is often better than scanning a package.
  • Use Spring test slices when the test targets one layer only.
  • Treat custom component scan rules as a precise testing tool, not the default answer to every context-loading issue.

Course illustration
Course illustration

All Rights Reserved.