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.