java - SpringBoot JUnit 和 @Value
问题描述
在我的配置类中,我有
@Value("${some.vars}")
private List<String> vars;
现在在我的测试中,我希望能够设置 this 的值,所以我有这个
@SpringBootTest
public class MyTest {
@Test
public void test() {
ApplicationContextRunner runner = new ApplicationContextRunner();
runner
.withConfiguration(AutoConfigurations.of(MyConfiguration.class))
.withUserConfiguration(UserConfiguration.class)
.withPropertyValues("some.vars=A,B,C")
.run(ctx -> {
// some test assertions
});
}
我将A,B,C
一个字符串绑定到List<String>
它的第 0 个位置。我希望它能够呈现并绑定为 List invars
解决方案
您需要额外的工作才能将字符串拆分为列表
@Value("#{'${some.vars}'.split(',')}")
private List<String> vars;
推荐阅读
- ios - 如何加载文本文件的内容并将其显示在 SwiftUI 文本视图中?
- r - 如何使用 tsibble 对象生成时间序列图?
- java - 为 Java 中的自定义异常抛出定义的消息
- c - 为什么使用信号处理程序在此代码中出现分段错误?
- java - 找到最大乘积的算法 - 分析
- c# - 在Unity2d中将rigidbody.velocity设置为鼠标的方向
- javascript - 函数可以在没有花括号的情况下工作,并且不能与它们一起工作,为什么?
- eclipse - HikariCP/Spigot:使用 Eclipse 无 maven 导出
- optimization - 使用soplex命令行输出解决方案
- javascript - Jest - 如何将其他文件中的变量/函数用于测试单元?