How to exclude AutoConfiguration classes in Spring Boot JUnit tests?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To exclude AutoConfiguration classes in Spring Boot JUnit tests, use the exclude attribute on @SpringBootTest, @EnableAutoConfiguration, or test-slice annotations like @WebMvcTest. You can also exclude them via spring.autoconfigure.exclude in a test-specific properties file. This gives you a leaner application context, faster test startup, and isolation from infrastructure dependencies you do not need for a given test.
Spring Boot's autoconfiguration is powerful in production, but during testing it often loads database drivers, message brokers, caches, and security filters that have nothing to do with the unit under test. A test for a REST controller should not fail because Cassandra is unreachable. Excluding irrelevant autoconfiguration classes solves that problem directly.
Method 1: @EnableAutoConfiguration(exclude)
The most explicit approach is placing the exclude attribute on @EnableAutoConfiguration. This works with any test that loads a Spring context.
This approach is straightforward because the exclusion is visible right next to the test class declaration. Anyone reading the test immediately understands which parts of the context are intentionally omitted.
Method 2: @SpringBootTest(exclude) or @SpringBootApplication(exclude)
If your test configuration class uses @SpringBootApplication, you can place the exclusion there instead.
Then reference this configuration in your test:
This pattern is useful when multiple test classes share the same set of exclusions. A dedicated test configuration class avoids repeating the exclusion list on every test.
Method 3: Test-Slice Annotations
Spring Boot's test-slice annotations like @WebMvcTest, @DataJpaTest, and @WebFluxTest already load only a subset of autoconfiguration. However, even within a slice, you sometimes need further exclusions.
Notice that @WebMvcTest uses excludeAutoConfiguration rather than exclude. The attribute name differs from @EnableAutoConfiguration, which is a common source of confusion.
Method 4: Properties-Based Exclusion
You can exclude autoconfiguration classes in application-test.properties or application-test.yml without touching annotations at all.
Activate this profile in your test:
This method keeps test classes clean and centralizes exclusion decisions. It is especially useful when an entire test suite needs the same exclusions.
Performance Impact of Exclusions
Excluding unnecessary autoconfiguration classes has a measurable impact on test execution time. Each autoconfiguration class that loads in a test context triggers bean creation, dependency injection, and potentially external resource initialization (database connections, message broker clients, cache pools). In a large Spring Boot application with many starters, the full autoconfiguration scan can add 10-30 seconds to context initialization.
Spring Boot caches application contexts between tests that share the same configuration. By keeping exclusion sets consistent across related test classes, you maximize context reuse and minimize cold-start overhead. If two test classes exclude different sets of autoconfiguration, Spring creates two separate contexts, which doubles the initialization cost.
When testing a specific layer (controllers, repositories, or services), prefer the built-in test slices (@WebMvcTest, @DataJpaTest, @WebFluxTest) over @SpringBootTest with exclusions. Test slices load only the autoconfiguration relevant to that layer, so you often need fewer manual exclusions.
Commonly Excluded AutoConfiguration Classes
| AutoConfiguration Class | What It Loads | Typical Reason to Exclude |
DataSourceAutoConfiguration | JDBC DataSource and connection pool | Tests that do not touch a database |
SecurityAutoConfiguration | Spring Security filter chain | Controller tests where auth is irrelevant |
CassandraAutoConfiguration | Cassandra session and template | Non-Cassandra tests in a multi-module project |
RedisAutoConfiguration | Redis connection factory | Tests that do not use caching or sessions |
MongoAutoConfiguration | MongoDB client and template | Services that do not depend on Mongo |
KafkaAutoConfiguration | Kafka producer/consumer setup | Tests for components unrelated to messaging |
FlywayAutoConfiguration | Flyway database migration runner | Unit tests where schema migration is unnecessary |
Verifying What Gets Loaded
If you are unsure which autoconfiguration classes are active, enable the autoconfiguration report.
Spring Boot will print a conditions evaluation report at startup, listing every autoconfiguration class and whether it was matched (loaded) or excluded. This is the fastest way to confirm your exclusions are working.
You can also check programmatically:
Common Pitfalls
Using the wrong attribute name on test-slice annotations. @WebMvcTest uses excludeAutoConfiguration, not exclude. Using the wrong attribute will compile but silently fail to exclude anything.
Excluding too many classes at once. Aggressive exclusion can cause the context to fail in unexpected ways when beans that do exist depend on excluded infrastructure. Exclude only what you actually need to remove, and let Spring Boot handle the rest.
Forgetting that @SpringBootTest without classes scans for the main @SpringBootApplication class. If your main class has its own exclusions, those apply in tests too. If it does not, you may need to specify a test-specific configuration class.
Not using @ActiveProfiles when relying on properties-based exclusion. The exclusions in application-test.properties only take effect when the "test" profile is active.
Confusing excludeName with exclude. Both exist on @EnableAutoConfiguration. The exclude attribute takes class references, while excludeName takes fully qualified class name strings. Use excludeName only when the class is not on your test classpath.
Summary
Spring Boot offers four ways to exclude autoconfiguration in tests: the exclude attribute on @EnableAutoConfiguration, the same attribute on @SpringBootApplication, the excludeAutoConfiguration attribute on test-slice annotations, and the spring.autoconfigure.exclude property in test configuration files. Use the annotation approach for targeted, per-test exclusions and the properties approach for suite-wide exclusions. Always verify your exclusions are working by enabling debug=true or asserting on the application context. The goal is a test context that loads only what the test actually needs, resulting in faster execution and fewer false failures from missing infrastructure.

