I'm building an extremly easy Spring Boot application. It has no code, just a simple test :
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTest {
    @Value("${my.key}")
    private String key;
    @Test
    public void test() {
        System.out.println(key);
    }
}
my.key is defined inside src/main/resources/application.properties (in main/ not test/)
The test doesn't pass because cannot find my.key property (but if I put this property inside src/test/resources/application.properties it works)
I'm sure I have seen plenty of Spring Boot applications where test classes read properties from /main/resources/application.properties
But here it does not work.
If you also add a application.properties file inside src/test/resources, this will "override" the one in src/main/resources, and hence none of the keys you define in your "production" properties file will be seen.
So either remove your application.properties in src/test/resources to use your property file in src/main/resources or define your values there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With