What's the difference between using @BeforeCla

2019-04-27 16:04发布

I'm new to unit test. About the purpose of using @Before annotation in JUnit 4. I just don't know the point of using it:

public class FoodTestCase {
    static private Food sandwich;

    @BeforeClass
    public static void initialise(){
        sandwich = new Sandwich();    
    }

}

vs

public class FoodTestCase {
    static private Food sandwich = new Sandwich();

}

What's the difference?

7条回答
孤傲高冷的网名
2楼-- · 2019-04-27 16:41

@BeforeClass is for static initializations.

Instances created here will be reused across all of your @Test s

Whereas @Before is per @Test .

Usually @BeforeClass is reserved for objects which are relatively expensive to instantiate.
e.g. Database connections

查看更多
登录 后发表回答