简体   繁体   English

Java Spring-如何通过@WithUserDetails使用application.properties中的值

[英]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.

相关问题 如何从Spring Boot中的application.properties中分配注释值? - How to assign annotation value from application.properties in Spring Boot? spring 引导中的@value 未从 application.properties 提供值 - @value in spring boot not giving value from application.properties Java Spring无法从application.properties中正确读取值 - Java spring does not read properly values from application.properties Spring Boot @Value 注解从 application.properties 中获取密钥,但在运行时不使用它 - Spring Boot @Value annotation picks up key from application.properties, but does not use it at run time 从application.properties读取值到Spring Batch Reader中? - Read value from application.properties into Spring Batch Reader? Spring 引导项目未绑定来自 application.properties 的 @Value - Spring Boot project not binding @Value from application.properties spring 从 bean 数据引导 application.properties 值 - spring boot application.properties value from bean data 属性值未使用 spring 引导从 application.properties 解析 - property value not resolved from application.properties using spring boot Spring:无法从 application.properties 获取价值 - Spring : Cannot get value from application.properties 如何在 Spring Boot application.properties 中使用 Eclipse 变量 - How to use Eclipse Variables in Spring Boot application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM