首页 > 解决方案 > @Value 在测试中不会被占位符替换

问题描述

我在一个弹簧启动应用程序中。我想进行一些单元测试并在 src/test/resources 中使用属性文件。

带有@Value注释的属性不会被属性值填充。

我试图添加<testResources>到 pom 中,但它不起作用。我尝试*在 classpath 关键字之后添加,@PropertySource但它也不起作用。

@Configuration
@PropertySource("classpath:application-test.properties")
public class UnitTestConfiguration {

}

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {UnitTestConfiguration.class})
class AccountsControllerTest {

    @Value("${web-client-factory.valid-jwt}")
    public String validJwt; // null in debug
}

如果 spring 没有找到属性,我预计会出现异常,但它什么也不做,我的属性是null.

标签: springmavenspring-bootjunit

解决方案


尝试使用:@TestPropertySource(locations={"application-test.properties"})


推荐阅读