JUnit 4 @BeforeClass & @AfterClass when using Suites

前端 未结 3 1628
梦毁少年i
梦毁少年i 2020-12-02 18:30

When using this approach below, by setting up the jUnit with Suites. We got the problem when all @BeforeClass in every Testclass will be executed before any tests starts to

相关标签:
3条回答
  • 2020-12-02 19:11

    I'm not too familiar with @RunWith in JUnit, so I may have done something wrong, but I can't seem to replicate the behaviour you describe. With the class:

    @RunWith(Suite.class)
    @Suite.SuiteClasses( { FirstTest.class, SecondTest.class, ThirdTest.class })
    public class AllTests {
        // empty
    }
    

    And FirstTest.java looking like this:

    public class FirstTest {
        @BeforeClass
        public static void doBeforeClass() {
             System.out.println("Running @BeforeClass for FirstTest");
        }
    
        @Test
        public void doTest() {
            System.out.println("Running @Test in " + getClass().getName());
        }
    }
    

    ... with SecondTest.java and ThirdTest.java pretty much the same. I get the test output:

    Running @BeforeClass for FirstTest
    Running @Test in FirstTest
    Running @BeforeClass for SecondTest
    Running @Test in SecondTest
    Running @BeforeClass for ThirdTest
    Running @Test in ThirdTest
    

    This is with JUnit 4.5.0 (default JUnit in Eclipse 3.5.1) on Sun's JDK 1.6.0_12. Can you spot any difference in my example from yours? Perhaps a different JDK/JVM? I don't know enough about the internals of JUnit to know if these can be a factor.

    0 讨论(0)
  • 2020-12-02 19:22

    Write a @BeforeClass method in AllTests class which will be executed when the suite is started.

    public class MyTests1 { 
        @BeforeClass
        public static void beforeClass() {
            System.out.println("MyTests1.beforeClass");
        }
    
        @Before
        public void before() {
            System.out.println("MyTests1.before");
        }
    
        @AfterClass
        public static void afterClass() {
            System.out.println("MyTests1.AfterClass");
        }
    
        @After
        public void after() {
            System.out.println("MyTests1.after");
        }
    
        @Test
        public void test1() {
            System.out.println("MyTests1.test1");
        }
    
        @Test
        public void test2() {
            System.out.println("MyTests1.test2");
        }
    }
    
    
    
    public class MyTests2 { 
        @BeforeClass
        public static void beforeClass() {
            System.out.println("MyTests2.beforeClass");
        }
    
        @Before
        public void before() {
            System.out.println("MyTests2.before");
        }
    
        @AfterClass
        public static void afterClass() {
            System.out.println("MyTests2.AfterClass");
        }
    
        @After
        public void after() {
            System.out.println("MyTests2.after");
        }
    
        @Test
        public void test1() {
            System.out.println("MyTests2.test1");
        }
    
        @Test
        public void test2() {
            System.out.println("MyTests2.test2");
        }
    }
    
    
    
    
    @RunWith(Suite.class)
    @Suite.SuiteClasses( { MyTests1.class, MyTests2.class })
    public class AllTests {
    
        @BeforeClass
        public static void beforeClass() {
            System.out.println("AllTests.beforeClass");
        }
    
        @Before
        public void before() {
            System.out.println("AllTests.before");
        }
    
        @AfterClass
        public static void afterClass() {
            System.out.println("AllTests.AfterClass");
        }
    
        @After
        public void after() {
            System.out.println("AllTests.after");
        }
    
        @Test
        public void test1() {
            System.out.println("AllTests.test1");
        }
    
        @Test
        public void test2() {
            System.out.println("AllTests.test2");
        }
    
    }
    

    OUTPUT

    AllTests.beforeClass
    MyTests1.beforeClass
    MyTests1.before
    MyTests1.test1
    MyTests1.after
    MyTests1.before
    MyTests1.test2
    MyTests1.after
    MyTests1.AfterClass
    MyTests2.beforeClass
    MyTests2.before
    MyTests2.test1
    MyTests2.after
    MyTests2.before
    MyTests2.test2
    MyTests2.after
    MyTests2.AfterClass
    AllTests.AfterClass
    

    hth

    0 讨论(0)
  • 2020-12-02 19:31

    I think, @BeforeClass executes at instanciation.

    0 讨论(0)
提交回复
热议问题