Search code examples
javatestingjunit4

Junit 4, how to disable creating new instance of Test per test method


Is there some way to disable creation new instance of Test per @Test ?


Solution

  • For the sake of making this an answer:

    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 );
        }
    }