java - Junit 在 Suite 类之间共享外部资源

标签 java junit

如何在 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/

相关文章:

java - 4D 数组的 DeepCopy/克隆问题

java - 断言等于 int long float

junit 中的 java.lang.NoClassDefFoundError

java - 由于未找到 Junit 测试报告,Jenkins 构建失败 : Intern Framework

java - 同步请求失败 HTTP

java - 如何使用 Apache Poi 设置 R1C1 样式公式

c# - 为接口(interface)使用 'I' 前缀或不使用

java - Jetty WebSocketException 无法调用方法

java - 如何模拟 @AfterParameter JUnit 注解?

java - 在 IntelliJ IDEA 中包含 JUnit 5 依赖项