Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

java - JUnit 4 @BeforeClass & @AfterClass when using Suites

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 execute. (For each n TestClass file the @BeforeClass runs, then after they have execute, it started to execute the first MyTest.class files @Test)

This will cause that we allocate up much resources and memory. My thoughts was that it must be wrong, shouldn't each @BeforeClass run only before the actual testclass is executed, not when the Suite is started?

@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
        ...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...