spring - @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
.
解决方案
尝试使用:@TestPropertySource(locations={"application-test.properties"})
推荐阅读
- python - Unpacking a lambda function I found to understand what it is doing python
- ios - 团队 ID 不匹配 - 测试密钥在 iOS 中仍然无法使用吗?
- pycharm - PyCharm 的项目目录在哪里?
- python-3.x - 有没有办法在 Falcon 的单个类资源下添加多个 on_get 方法?
- javascript - 无法将数据 jquery 发布到 phpmailer
- git - Git跟踪不同的分支以进行推送和拉取?
- node.js - 为什么 fs.open 返回一个数字?
- sql-server - 如何在 SQL Server 中进行空间重投影
- java - 在命令行上使用 junit-4.12.jar 和 hamcrest-core-1.3.jar 编译和运行测试?错误:是抽象的,无法实例化,找不到符号
- multithreading - 在线程之间传输数据而不在 D 中复制