Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: @TestConfiguration Not Overriding Bean During Integration Test

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.

like image 363
The Head Rush Avatar asked May 30 '18 14:05

The Head Rush


People also ask

How do you override a bean in a spring boot?

Bean Overriding Spring beans are identified by their names within an ApplicationContext. Therefore, bean overriding is a default behavior that happens when we define a bean within an ApplicationContext that has the same name as another bean. It works by simply replacing the former bean in case of a name conflict.

How do you use TestConfiguration?

Using @TestConfiguration in Unit Tests As explained earlier, we can use the @TestConfiguration annotation in two ways during testing: Import test configuration using the Import annotation. Declaring @TestConfiguration as a static inner class.

Is SpringBootTest integration test?

Spring boot provides @SpringBootTest annotation which starts the embedded server, creates a web environment and then enables @Test methods to do integration testing. Use it's webEnvironment attribute for it. It also creates the ApplicationContext used in our tests.


1 Answers

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.

like image 55
Sam Brannen Avatar answered Sep 19 '22 09:09

Sam Brannen