[英]Java Spring - How to use value from application.properties with @WithUserDetails
I want to use a value from application.properties
with the @WithUserDetails()
annotation for my tests. 我想在我的测试中使用带有
@WithUserDetails()
批注的application.properties
的值。
My current code: 我当前的代码:
@Value("${user.admin}")
private String ADMIN;
@Test
@WithUserDetails(ADMIN)
public void foo() {
}
displays the error "Attribute value must be constant" 显示错误“属性值必须恒定”
I am using junit4 with spring runner 我正在使用带有弹簧转轮的junit4
似乎无法做到这一点,Java编译器不允许这样做,因为它在编译时不将@Value()
的值视为常量。
java has built in capabilities to read a .properties file and JUnit has built in capabilities to run setup code before executing a test suite. Java内置了读取.properties文件的功能,而JUnit内置了在执行测试套件之前运行安装代码的功能。
java reading properties: java阅读属性:
Properties p = new Properties();
p.load(new FileReader(new File("application.properties")));
junit startup documentation junit启动文档
put those 2 together and you should have what you need. 将那两个放在一起,您应该拥有所需的东西。
But yes the minimum requirment is you have application.properties
file in your test->resources
package as well. 但是,是的,最低要求是在
test->resources
包中也有application.properties
文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.