java - 如果@before在构造函数之后执行,那么如何初始化该类?

标签 java constructor junit4 oracle-adf

我正在 Oracle ADF 中准备一些 JUnit 测试,我很惊讶,因为我看到测试类的构造函数在 @before setup() 方法之前执行...

问题是构造函数需要初始化变量,我从设置方法中将这些变量设置为构造函数...从逻辑上讲,我总是得到一个漂亮的 NullPointerException...

我能看到的唯一解决方案是直接从构造函数初始化变量,但我发现这非常难看

有什么想法吗?提前感谢大家

最佳答案

也许你可以使用 JUnit 的 @BeforeClass注释。

Sometimes several tests need to share computationally expensive setup (like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization. Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those the current class.

@Before :

When writing tests, it is common to find that several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before the Test method. The @Before methods of superclasses will be run before those of the current class.

关于java - 如果@before在构造函数之后执行,那么如何初始化该类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25790662/

相关文章:

c# - 静态工厂方法与公共(public)构造函数

java - 字符串作为新类的唯一私有(private)字段

java - 在 Eclipse 中运行 Web 服务器

java - 使用 Tomcat 和 cookie 进行 session 管理

java - 不带精度和逗号的双数乘法

java - Windows 7 开发机器上的 Quartz 调度程序和 Linux 机器上的生产环境

Typescript构造函数,这两种构造对象的方法是等价的吗?

java - 如果在执行期间抛出具体异常,如何为 junit 创建自己的注释将跳过测试?

java - Sonarqube 测试用例规则不考虑 @Test 注释

java - 如何在不复制代码的情况下测试 Junit5 中接口(interface)的不同实现