6

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.

DanielM
  • 1,106
  • 3
  • 17
  • 27
  • i didn't see where you used this property – Jaiwo99 Nov 16 '16 at 14:04
  • Sorry, you are right I didn't clarify that. It is being used in the REST controllers to define the base path for all the end-points. There the property is doing just fine -- as I mentioned, I can access them from a browser as expected. The only place it cannot be read from are the tests. – DanielM Nov 16 '16 at 14:12
  • It seems like Intellij did not recognize /resources as resources folder. Try @PropertySource('file:/path/to/application.properties') – Issam El-atif Nov 16 '16 at 15:23

2 Answers2

2

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.

DanielM
  • 1,106
  • 3
  • 17
  • 27
0

Your application.properties must not be on the test classpath. You should define a "Test Resources" location for your project in IntelliJ and create an application.properties file there.

BTW, I've found it to be useful to have a separate test properties file, since test properties are typically different than the normal run-time environment.

Kevin Condon
  • 1,618
  • 10
  • 16
  • I've tried to mark the resources folder as "Test Resources Root" on IntelliJ, but that didn't help. – DanielM Nov 16 '16 at 14:22
  • When you look at the actual files in the test classpath, what do you see? Unfortunately, I've never used IntelliJ so I have limited insight there. Maybe http://stackoverflow.com/questions/17039653/resources-not-copied-to-output-path-in-intellij-12-1-4 helps? – Kevin Condon Nov 16 '16 at 14:31