有什么方法可以禁用每个@Test创建Test的新实例吗?

最佳答案

为了使这个答案:

public class MyTestClass {
    private static String onceForAllTests;

    @AfterClass
    public static void afterClass() {
        onceForAllTests = null; // silly, but just to demonstrate
    }

    @BeforeClass
    public static void beforeClass() {
        onceForAllTests = "This is set once for all tests";
    }

    @Test
    public void sillyTest {
        String someTestValue = "This is set during method";
        assertNotEquals( onceForAllTests, someTestValue );
    }
}

08-27 09:35