9

I'm writing integration test for my spring boot application but when I try to override some properties using @TestPropertySource, it's loading the property file defined in the context xml but it's not overriding the properties defined in the annotation.

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {DefaultApp.class, MessageITCase.Config.class})
@WebAppConfiguration
@TestPropertySource(properties = {"spring.profiles.active=hornetq", "test.url=http://www.test.com/",
                    "test.api.key=343krqmekrfdaskfnajk"})
public class MessageITCase {
    @Value("${test.url}")
    private String testUrl;

    @Value("${test.api.key}")
    private String testApiKey;

    @Test
    public void testUrl() throws Exception {
        System.out.println("Loaded test url:" + testUrl);
    }



    @Configuration
    @ImportResource("classpath:/META-INF/spring/test-context.xml")
    public static class Config {

    }
}
kosker
  • 333
  • 1
  • 3
  • 12
  • 1
    did you resolve this problem? inline properties through TestPropertySource do not seem to be working for me either. – Nishith Nov 26 '15 at 11:14
  • Not yet, but I've changed my configuration so that I'm using `@IntegrationTest` annotation instead of `@TestPropertySource`. I'll post an answer soon. – kosker Nov 27 '15 at 14:10

2 Answers2

3

I tested this feature with Spring Boot 1.4 Line below works pretty well

@TestPropertySource(properties = { "key=value", "eureka.client.enabled=false" })

Nevertheless new @SpringBootTest annotation is working as well

@RunWith(SpringRunner.class)
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    properties = { "key=value", "eureka.client.enabled=false" }
)
public class NewBootTest {

    @Value("${key}")
    public String key;

    @Test
    public void test() {
        System.out.println("great " + key);
    }
}
1

I had a similar problem. I fixed it by updating the Spring Context beans.xml to use org.springframework.context.support.PropertySourcesPlaceholderConfigurer instead of org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.

From the JavaDoc of PropertyPlaceholderConfigurer

Deprecated. as of 5.2; use org.springframework.context.support.PropertySourcesPlaceholderConfigurer instead which is more flexible through taking advantage of the Environment and PropertySource mechanisms.

ab2000
  • 364
  • 2
  • 8