Below is my setup to test the issue. I have setup a simple Springboot Application called Application.java. My test is called DemoTest.java which loads the default properties specified from my Springboot application defined in application.properties. I'm using the @TestPropertySource annotation to override any properties defined by the application. I had expected the properties defined in my test.properties file to override the application properties but that is not the case. Base on javadocs for @TestPropertySource, this should work. If I specify the property inline in @TestPropertySource, it does override. However, this is not ideal if I have multiple properties or multiple tests that needs to override the default application properties.
application.properties my.property=false
test.properties my.property=true
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class, webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations="classpath:test.properties")
public class DemoTest {
@Autowired
private Environment env;
@Test
public void test() {
// test.properties for property "my.property=true" but pulls false from the application.properties
assertEquals(Boolean.TRUE, Boolean.valueOf(env.getProperty("my.property")));
}
}