Spring Boot MockMVC Test does not load Yaml file

I have my configuration in the application.yml file in the classpath root folder (src / main / resources /). The configuration loads normally when I run the application normally. However, in my test, the application.yml file does not load at all.

The title of my test is as follows:

@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = Configuration.class) @org.junit.Ignore public class ApplicationIntegrationTest { @Inject private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } ... 

Configuration class:

 @EnableAutoConfiguration @ComponentScan("cetsweb, cetsservice") public class Configuration extends WebMvcConfigurerAdapter { 

When I debug the application, I see that the yml files are loaded in the ConfigFileApplicationListener, however in the test, however, the ConfigFileApplicationListener is not called.

+5
source share
2 answers

a whole chapter in Spring's download guide for testing. This section explains how to run a basic test for a Spring boot application.

In short, when using Spring Boot and you want to run the test, you need to use the @ SpringApplicationConfiguration annotation instead of the @ContextConfiguration annotation. @SpringApplicationConfiguration is a specialized @ContextConfiguration extension that registers / loads some of the Spring boot mats for test cases.

+5
source

There is good integration between StringBoot, jUnit and YAML.

 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(MainBootApplication.class) public class MyJUnitTests { ... } @Configuration @EnableConfigurationProperties @ConfigurationProperties(prefix = "section1") public class BeanWithPropertiesFromYML { ... } 

Read more here: fooobar.com/questions/294077 / ...

0
source

All Articles