测试环境中的Spring Boot PropertySources

时间:2015-12-10 11:56:36

标签: java spring spring-boot spring-test

我有一个application.properties我试图在bean中自动引导的值

@Component
public class ConfigurationValues {
    @Value("${my.host}") private String myHost
}

当我运行应用程序时,这很有效。但是,当我运行JUnit测试时,我收到错误

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'my.host' in string value "${my.host}"

这是我的测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {Application.class})
public class AnotherDummyTest {

    @Autowired
    private ConfigurationValues configurationValues;

    @Test
    public void testValue() {
        Assert.assertEquals("localhost", configurationValues.getMyHost());
    }
}

此测试用例因我提到的IllegalArgumentException而失败。

1 个答案:

答案 0 :(得分:1)

试试这个:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class,
    initializers = ConfigFileApplicationContextInitializer.class)
public class AnotherDummyTest {