如何在 JUnit 5 测试中填充 yml 属性文件而不构建 spring 上下文

发布于 2025-01-18 16:20:39 字数 1554 浏览 0 评论 0原文

假设我有一个服务从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2025-01-25 16:20:39

首先,类型应该是整数。

@Value("${orders.batch-size}")
private Integer ordersBatchSize;

那么使用反射实用程序是一种非常常见的方法。请注意,字段名称必须匹配。

ReflectionTestUtils.setField(classUnderTest, "ordersBatchSize", 100)

更好的方法是在用 @BeforeEach 注释的方法中使用 setter(如果可用)。

@BeforeEach
public void beforeEach() {
    classUnderTest.setOrdersBatchSize(100);
}

最后,最好的方法是在 Spring Boot 优先使用的 test/resources 文件夹中定义 application.yml 以便进行测试:

orders.batch-size=100

First of all, the type should be an integer.

@Value("${orders.batch-size}")
private Integer ordersBatchSize;

Then using the reflections utils is quite a common approach. Note the field name has to match.

ReflectionTestUtils.setField(classUnderTest, "ordersBatchSize", 100)

A better way is using a setter (if available) in the method annotated with @BeforeEach.

@BeforeEach
public void beforeEach() {
    classUnderTest.setOrdersBatchSize(100);
}

Finally, the best way is to define application.yml in the test/resources folder which Spring Boot takes with priority for sake of testing:

orders.batch-size=100
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文