gpt4 book ai didi

java - JUnit 测试方法无法返回值

转载 作者:行者123 更新时间:2023-12-05 08:39:13 25 4
gpt4 key购买 nike

为什么 JUnit 测试方法不能返回值?

Documentation说(强调我的):

Test methods and lifecycle methods may be declared locally within the current test class, inherited from superclasses, or inherited from interfaces (see Test Interfaces and Default Methods). In addition, test methods and lifecycle methods must not be abstract and must not return a value.

为什么要这样强制执行

这纯粹是一种设计选择吗?这是最佳做法吗?

最佳答案

我还没有看到提到的一件事是,这个设计决定背后的原因之一是历史原因。

在用注解标记的测试用例之前,它们由 JUnit 通过反射分析 - 所有 public void 方法,其名称以 test 开头,但没有t 有任何参数,被认为是框架运行的测试用例。

如果这些方法也允许返回一个值,您就不会创建一个以 test 开头的公共(public)辅助方法来计算某些测试中常用的值,否则框架运行器会尝试运行它。

我想无论如何你都应该将它们设为私有(private)。

此外,这也是“以动词开头的方法名称应该某事”(而不是计算某事)的高峰期。

关于java - JUnit 测试方法无法返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60654124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com