如何在 JUnit 5 测试中填充 yml 属性文件而不构建 spring 上下文
假设我有一个服务从 application.yml
文件中获取属性文件:
orders:
batch-size: 2
并通过 @Value 注释读取:
@Value("${orders.batch-size}")
private String ordersBatchSize;
以进行数据分区。
我正在编写 JUnit 测试,并希望从资源中的 application.yml 文件获取上述属性。问题是 JUnit 测试属性始终为 null。我的 JUnit 类测试用 @ExtendWith(MockitoExtension.class)
进行注释。 我不想通过使用 @SpringBootTest 注释类来构建 Spring 上下文。 根据 this,我需要填充属性文件,但我可能尝试了所有方法,但没有得到理想的结果。
到目前为止的尝试:
ReflectionTestUtils.setField(classUnderTest, "field", "value")
或
Yaml yaml = new Yaml();
InputStream inputStream =this.getClass().getClassLoader().getResourceAsStream("application.yml");
yaml.load(inputStream);
或
Properties properties = new Properties();
properties.load(new FileReader("/application.properties"));
或
System.setProperty("orders.batch-size", "2");
或:
public static Properties fetchProperties() {
Properties properties = new Properties();
try {
File file = ResourceUtils.getFile("classpath:application.yml");
InputStream in = new FileInputStream(file);
properties.load(in);
} catch (IOException e) {
// exception logic
}
return properties;
}
或此主题的提示,但没有带来理想的解决方案。
对于如何解决此问题的建议,我将不胜感激。干杯!
Let's say that I have a service that took a property file from the application.yml
file:
orders:
batch-size: 2
and read by @Value annotation:
@Value("${orders.batch-size}")
private String ordersBatchSize;
to conduct data partitioning.
I am writing the JUnit tests and would like to get the above property from the application.yml
file which is in the resources. Problem is that the JUnit test property is always null. My JUnit class test is annotated with @ExtendWith(MockitoExtension.class)
.
I don't want to build Spring context by annotating the class with @SpringBootTest
.
According to this, I need to populate the property file but I tried probably everything without a desirable result.
Attempts so far:
ReflectionTestUtils.setField(classUnderTest, "field", "value")
or
Yaml yaml = new Yaml();
InputStream inputStream =this.getClass().getClassLoader().getResourceAsStream("application.yml");
yaml.load(inputStream);
or
Properties properties = new Properties();
properties.load(new FileReader("/application.properties"));
or
System.setProperty("orders.batch-size", "2");
or:
public static Properties fetchProperties() {
Properties properties = new Properties();
try {
File file = ResourceUtils.getFile("classpath:application.yml");
InputStream in = new FileInputStream(file);
properties.load(in);
} catch (IOException e) {
// exception logic
}
return properties;
}
or hints from this topic but it didn't bring a desirable solution.
I will be grateful for suggestions on how to fix this issue. Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,类型应该是整数。
那么使用反射实用程序是一种非常常见的方法。请注意,字段名称必须匹配。
更好的方法是在用
@BeforeEach
注释的方法中使用 setter(如果可用)。最后,最好的方法是在 Spring Boot 优先使用的
test/resources
文件夹中定义application.yml
以便进行测试:First of all, the type should be an integer.
Then using the reflections utils is quite a common approach. Note the field name has to match.
A better way is using a setter (if available) in the method annotated with
@BeforeEach
.Finally, the best way is to define
application.yml
in thetest/resources
folder which Spring Boot takes with priority for sake of testing: