如何在 junit 测试套件中的两个类之间共享 ExternalResource
?
TestSuite.java
@RunWith(Suite.class)
@SuiteClasses({ TestA.class, TestB.class })
public class TestSuite {
@ClassRule
public static MyResource res = new MyResource();
}
MyResource
是一个 ExternalResource
,我想访问测试套件类 TestA 和 TestB 中的 res
。
TestA.java
public class TestA {
@Test
public void testA() {
System.out.println("####### testA");
// res.someGetMethod();
}
}
TestB.java
public class TestB {
@Test
public void testB() {
System.out.println("####### testB");
// res.someGetMethod();
}
}
感谢您的浏览。
最佳答案
套件的 ClassRules
是共享的,可以从其 TestCases
访问,它将在测试用例之前初始化,因此您可以像访问任何公共(public)实例一样访问它。静态变量
@Test
public void testA() {
System.out.println("####### testA");
TestSuite.res.someGetMethod();
}
关于java - Junit 在 Suite 类之间共享外部资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34552097/