samedi 30 juillet 2016

Junit: How to make every @Test methods isolated completely?

Example code:

public class Count {
    static int count;

    public static int add() {
       return ++count;
    } 
}

I want test1 and test2 run totally separately so that they both pass. How can I finish that? My IDE is Intellij IDEA.


public class CountTest {
    @Test
    public void test1() throws Exception {
        Count.add();
        assertEquals(1, Count.count);//pass.Now count=1
    }

    @Test
    public void test2() throws Exception {
        Count.add();
        assertEquals(1, Count.count);//error, now the count=2
    }
}

Assume the test1 runs before test2.

This is just a simplified code. In fact the code is more complex so I can't just make count=0 in @after method.

Aucun commentaire:

Enregistrer un commentaire