Java Spring Boot Test How to exclude java configuration class from test context
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
When running Spring Boot tests, you often need to exclude certain configuration classes that interfere with testing — database configs, external service clients, or security configurations. Spring Boot provides several ways to do this: @SpringBootTest(exclude=...) for auto-configuration, @TestConfiguration for overrides, @Import for selective loading, and @MockBean for replacing individual beans. The right approach depends on whether you are excluding an auto-configuration class or a custom @Configuration class.
Excluding Auto-Configuration Classes
For Spring Boot auto-configuration classes (like DataSourceAutoConfiguration):
Or in application-test.properties:
Excluding Custom @Configuration Classes
For your own @Configuration classes, the approach is different because exclude only works with auto-configuration:
Method 1: Use @Profile
The @Profile("!test") annotation means "load this configuration in every profile except test."
Method 2: @TestConfiguration Override
@TestConfiguration beans override production beans with the same name and type.
Method 3: @ComponentScan.Filter
Method 4: Selective @Import
Instead of loading the full application context, import only what you need:
Using @MockBean to Replace Beans
Instead of excluding entire configurations, mock individual beans:
@MockBean adds a Mockito mock to the Spring context, replacing any existing bean of that type.
@WebMvcTest with Controller Isolation
@WebMvcTest only loads the specified controller and web-related beans. All service and repository beans must be mocked.
@DataJpaTest for Repository Isolation
Conditional Configuration Loading
Common Pitfalls
- Using
excludefor custom@Configurationclasses: Theexcludeparameter on@SpringBootTestand@EnableAutoConfigurationonly works for auto-configuration classes (those listed inMETA-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports). For custom configs, use@Profile,@TestConfiguration, or@ComponentScan.Filter. @TestConfigurationnot being picked up:@TestConfigurationclasses in separate files are not automatically scanned. You must add@Import(TestConfig.class)to your test or place the inner@TestConfigurationclass inside the test class.@MockBeanreplacing the wrong bean: If you have multiple beans of the same type,@MockBeanreplaces all of them. Use@Qualifierto target a specific bean.- Context caching issues: Spring caches test application contexts. Changing which configs are excluded creates a new context, slowing down the test suite. Group tests with the same context configuration.
@ActiveProfiles("test")not working: Ensure@Profile("!test")is on the config class, not on individual@Beanmethods (unless you want fine-grained control). Profile annotations on@Configurationaffect all beans in that class.
Summary
- Use
@EnableAutoConfiguration(exclude = ...)for Spring Boot auto-configuration classes - Use
@Profile("!test")+@ActiveProfiles("test")for custom@Configurationclasses - Use
@TestConfigurationwith@Importto override production beans in tests - Use
@MockBeanto replace individual beans with Mockito mocks - Use slice tests (
@WebMvcTest,@DataJpaTest) to load only relevant beans - Prefer narrow test contexts over full
@SpringBootTestwhen you only need a few beans
Related reading
- Java Spring REST API Handling Many Optional `Parameters`
- Java Spring Security - User.withDefaultPasswordEncoder is deprecated?
- Java Stanford NLP Part of Speech labels?
- Java Static vs inner class
- Java verify void method calls n times with Mockito
- java.lang.NoSuchMethodError org.mockito.MockingDetails.getMockCreationSettingsLorg/mockito/mock/MockCreationSettings
- Java Stream API - Best way to transform a list map or forEach?
- Java String array is there a size of method?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.