JUnit 4 @BeforeClass&使用套房时@AfterClass

时间:2009-12-17 12:42:10

标签: java junit junit4

在下面使用此方法时,通过设置带套件的jUnit。在任何测试开始执行之前,每个Testclass中的所有@BeforeClass都会被执行,我们遇到了问题。 (对于每个运行@BeforeClass的n个TestClass文件,然后在执行之后,它开始执行第一个MyTest.class文件@Test)

这将导致我们分配大量资源和内存。 我的想法是它一定是错的,不应该每个@BeforeClass只在执行实际的测试类之前运行,而不是在套件启动时运行吗?

@RunWith(Suite.class)
@Suite.SuiteClasses({ MyTests.class, Mytests2.class, n1, n2, n })
public class AllTests {
    // empty
}


public class MyTests {  // no extends here
    @BeforeClass
    public static void setUpOnce() throws InterruptedException {
        ...
    @Test
        ...

public class MyTests2 {  // no extends here
    @BeforeClass
    public static void setUpOnce() throws InterruptedException {
        ...
    @Test
        ...

3 个答案:

答案 0 :(得分:50)

在AllTests类中编写一个@BeforeClass方法,该方法将在套件启动时执行。

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

}

<强>输出

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

答案 1 :(得分:1)

我对JUnit中的@RunWith不太熟悉,所以我可能做错了什么,但我似乎无法复制你描述的行为。上课:

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

和FirstTest.java看起来像这样:

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

...与SecondTest.java和ThirdTest.java几乎相同。我得到了测试结果:

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

这是Sun的JDK 1.6.0_12上的JUnit 4.5.0(Eclipse 3.5.1中的默认JUnit)。你能发现我的例子与你的不同吗?也许是一个不同的JDK / JVM?我对JUnit的内部结构知之甚少,不知道这些是否是一个因素。

答案 2 :(得分:-4)

我认为,@BeforeClass会在实施时执行。