【问题标题】:@Value isn't replace by placeholder in test@Value 在测试中不会被占位符替换
【发布时间】:2019-06-04 11:08:57
【问题描述】:

我正在使用 Spring Boot 应用程序。我想进行一些单元测试并在 src/test/resources 中使用属性文件。

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

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

@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,我预计会出现异常。

【问题讨论】:

    标签: spring maven spring-boot junit


    【解决方案1】:

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

    【讨论】:

    • 我尝试通过@TestPropertySource(locations={"application-test.properties"}) 更改@PropertySource,但没有成功:s validJWT 属性仍然为空
    • 尝试将@SpringBootTest 添加到 AccountsControllerTest 并判断它是否有效!
    • 发生了一些不同的事情,我的属性等于我的密钥“${web-client-factory.valid-jwt}”。我需要启用属性没有?
    • 哦,我有这个想法,但我为时已晚:(因为你的属性看起来像 application-{profile}.properties
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 2012-10-01
    • 2016-01-20
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 2013-04-08
    相关资源
    最近更新 更多