@Value "Could not resolve placeholder" in Spring Boot Test
You need to add
@PropertySource("classpath:application.properties")
to your class, so it will pick your normal configurations.
If you need different configurations for test you can add
@TestPropertySource(locations="classpath:test.properties")
If not just copy paste your config file to test/resources
folder, then boot will pick from there.
See this.
You can use the @SpringBootTest
that will do create the PropertySourcesPlaceholderConfigurer
automatically.
This is described in the Testing chapter of the Spring Boot documentation.
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-configfileapplicationcontextinitializer-test-utility
You have annotated your test class with @ContextConfiguration(classes = {ApplicationTest.class})
. Wherein ApplicationTest.class
does the component scan on a mentioned package. When you run your test it tries to find the configuration from the resources folder in 'main' instead of 'test'. If you annotate your class with @SpringBootTest(classes = {ClassToBeTested.class})
or just @SpringBootTest
in this particular case, I think (not 100% sure) it will create a limited context and pick up the properties from test/resources.
If your properties are test specific, you can name your properties/yml file as application-test.properties
or application-test.yml
. And use @ActiveProfiles("test")
in your test class so that it will always read test specific properties file.
I usually use this solution which works for me.