Spring Boot: @TestConfiguration Not Overriding Bean During Integration Test

spring boot test configuration override bean
spring boot test exclude configuration
spring boot testconfiguration
spring boot test configuration properties
spring boot test conditionalonproperty
autowired junit tests with spring boot
spring boot test bean not found
spring boot test-autoconfigure

I have a Bean defined in a class decorated with @Configuration:

@Configuration
public class MyBeanConfig {

    @Bean
    public String configPath() {
        return "../production/environment/path";
    }
}

I have a class decorated with @TestConfiguration that should override this Bean:

@TestConfiguration
public class MyTestConfiguration {

    @Bean
    @Primary
    public String configPath() {
        return "/test/environment/path";
    }
}

The configPath bean is used to set the path to an external file containing a registration code that must be read during startup. It is used in an @Component class:

@Component
public class MyParsingComponent {

    private String CONFIG_PATH;

    @Autowired
    public void setCONFIG_PATH(String configPath) {
        this.CONFIG_PATH = configPath;
    }
}

While trying to debug this, i set a breakpoint inside each method as well as the constructor of the test config class. The @TestConfiguration's constructor breakpoint is hit, so i know that my test configuration class instantiates, however the configPath() method of that class is never hit. Instead, the configPath() method of the normal @Configuration class is hit and the @Autowired String in MyParsingComponent is always ../production/environment/path rather than the expected /test/environment/path.

Not sure why this is happening. Any thoughts would be greatly appreciated.


As documented in the Detecting Test Configuration section of the Spring Boot reference manual, any beans configured in a top-level class annotated with @TestConfiguration will not be picked up via component scanning. So you have to explicitly register your @TestConfiguration class.

You can do that either via @Import(MyTestConfiguration.class) or @ContextConfiguration(classes = MyTestConfiguration.class) on your test class.

On the other hand, if your class annotated with @TestConfiguration were a static nested class within your test class, it would be registered automatically.

Overriding beans in Integration tests, Since Spring Boot 1.4.x there is an option to use @MockBean annotation to fake To keep context in cache do not use @DirtiesContext , but use specify only a subset of your beans configuration in the test configuration. One shouldn't need this work modern Spring Boot - nested configuration classes should be picked up automatically. – Boris the Spider Dec 17 '18 at 9:37 If possible I'd like to keep the configuration inside the test class itself.


Make sure that the method name of your @Bean factory method does not match any existing bean name. I had issues with method names like config() or (in my case) prometheusConfig() which collided with existing bean names. Spring skips those factory methods silently and simply does not call them / does not instantiate the beans.

If you want to override a bean definition in your test, use the bean name explicitly as string parameter in your @Bean("beanName") annotation.

Override bean in Spring Boot 2.1 slice test with nested , One of my test is failing due to a change to bean overriding default. I tried to Override bean in Spring Boot 2.1 slice test with nested TestConfiguration #17830 in my case because a slice test is not really an integration test. The spring-boot-starter-test is the primary dependency that contains the majority of elements required for our tests. The H2 DB is our in-memory database. It eliminates the need for configuring and starting an actual database for test purposes. 4. Integration Testing With @DataJpaTest


Spring Boot, JAX-RS Integration. Integrating In Spring Boot, @TestConfiguration annotation can be used to define/override beans for unit tests. ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type� Spring boot integration test rollback does not work. Refresh. April 2019. Views. 1.6k time. 1. In spring boot I'm trying to create my first transactional test, but


Spring boot test configuration - @TestConfiguration, Learn to create and import test configuration in spring boot applications with If XML or other non-configuration bean definition resources need to be imported, use and beans while performing unit and integration testing. With Spring Boot 2.1 bean overriding is disabled by default, which is a good thing. However I do have some tests where I replace beans with mocked instances using Mockito. With the default setting Tests with such a configuration will fail due to bean overriding. The only way I found worked, was to enable bean overriding through application


14. Integration Testing - Project Metadata API Guide, The Spring Framework provides first-class support for integration testing in This testing does not rely on an application server or other deployment environment. composed annotation that centralizes the common test configuration like this: Beans defined in "extended-config.xml" may therefore override (i.e., replace)� The Spring Framework provides first-class support for integration testing in the spring-test module. The name of the actual JAR file might include the release version and might also be in the long org.springframework.test form, depending on where you get it from (see the section on Dependency Management for an explanation).


Testing - Project Metadata API Guide, Support Classes for Integration Testing (A thorough treatment of testing in the enterprise is beyond the scope of For example, if you have configured a bean as a dynamic mock by If you need to merge or override the configuration for a given level of @SpringJUnitConfig(TestConfig.class) (1) class� Note: If you have to use Spring Framework 3.1, you can write unit tests for your controllers by using spring-test-mvc. This project was included in the spring-test module when Spring Framework 3.2 was released. Let’s move on and take a quick look at our example application. The Anatomy of Our Example Application