Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot integration tests cannot reach application.properties file

I have some classes annotated with @RestController that I'm trying to test using the MockMvc class. The end-points respond properly from a web application, but I get the following error when running the 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"

This is how 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.

This is how the 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 the REST controllers are implemented (where the problematic property is being used):

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

This is how the application's main class looks like:

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

And finally, this is (a subset of) the project's 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've found several solutions to the problem throughout the web, but none of them seemed to work for me.

like image 980
DanielM Avatar asked Nov 16 '16 14:11

DanielM


1 Answers

The answer was finally not related with Spring annotations nor IntelliJ configuration, but rather with MockMvc and, in particular, with the class and method MockMvcBuilders.standaloneSetup, which was being used on the tests' setUp. This would not use the application's context, thus not being able to properly read properties which depend on it.

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

Build[s] a MockMvc instance using the given, fully initialized (i.e., refreshed) WebApplicationContext.

everything worked fine. For integration tests, it makes more sense to use this, doesn't it?

Thanks to all for your time and effort. Sorry for not showing the mentioned setUp method, but I didn't imagine the problem could have been caused there.

like image 96
DanielM Avatar answered Oct 26 '22 10:10

DanielM