gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:28:18 25 4
gpt4 key购买 nike

我正在 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/

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