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.