gpt4 book ai didi

java - @ClassRule 适用于所有测试类

转载 作者:太空宇宙 更新时间:2023-11-04 09:14:28 24 4
gpt4 key购买 nike

我有 ClassRule,例如 myRule,我想将此类规则应用于所有测试类。我使用 spring(集成测试)junit 4。

如果:我必须添加所有类此语句:

@ClassRule 
public static MyRule myRule = MyImplRule.getInstance();

有没有办法不在所有测试类中都写这个语句,而是应用所有测试类?

最佳答案

我认为你应该看这里:

How to run a set of code before and after any tests in any class?

适合您的情况的最佳示例是声明一个测试套件:

@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class })
public class AllTests {
@ClassRule
public static MyRule myRule = MyImplRule.getInstance();
}

该解决方案还展示了为每个测试运行代码的一些其他方法。已经很不错了。

关于java - @ClassRule 适用于所有测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59238350/

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