Spring Boot integration tests cannot get application.properties file

I have some classes annotated with @RestController that I am trying to test with the MockMvc class. Endpoints respond properly from a web application, but the following error occurs when running tests (from IntelliJ IDEA):

 java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.data.rest.base-path' in string value "${spring.data.rest.base-path}/whatever" 

Here's what the application.properties file looks like:

 spring.data.rest.base-path=/api spring.profiles.active=dev ... 

I also have a file called application-dev.properties with additional (different) properties.

Here's how test classes are annotated:

 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebIntegrationTest // Also tried: @WebAppConfiguration @ActiveProfiles("dev") // Also tried: @PropertySource("classpath:application.properties") // Also tried: @TestPropertySource("classpath:application.properties") public class MyRestControllerTest { ... } 

On the other hand, this is how REST controllers are implemented (where the problematic property is used):

 @RestController @RequestMapping("${spring.data.rest.base-path}/whatever") public class MyRestController { ... } 

This is what the main class of the application looks like:

 @SpringBootApplication(scanBasePackages = {...}) @EnableJpaRepositories({...}) @EntityScan({...}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 

And finally, this (subset) of the project structure:

 my-project |_ src |_ java | |_ com.example.x | |_ controller | |_ MyRestController.java | |_ test | |_ com.example.x | |_ controller | |_ MyRestControllerTest.java | |_ resources |_ application.properties |_ application-dev.properties 

I found several solutions to the problem throughout the network, but none of them seemed to work for me.

+5
source share
2 answers

The answer, finally, was not related to Spring annotations or IntelliJ configuration, but rather to MockMvc and, in particular, to the class and method MockMvcBuilders.standaloneSetup , which was used in setUp tests. This will not use the application context, so it will not be able to correctly read properties that depend on it.

After changing it to MockMvcBuilders.webAppContextSetup , which (from the docs)

Build [s] an instance of MockMvc using the specified, fully initialized (i.e. updated) WebApplicationContext.

everything worked fine. For integration tests, it makes sense to use this, right?

Thank you all for your time and effort. Sorry for the lack of the specified setUp method, but I did not think that the problem could have been caused there.

+1
source

Your application.properties should not be in the test path. You must locate the “Testing Resources” location for your project in IntelliJ and create the application.properties file there.

By the way, I found it useful to have a separate test properties file, since test properties usually differ from the usual runtime.

0
source

Source: https://habr.com/ru/post/1259839/


All Articles