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:21

In this case it may not be necessary, as the initialization is really simple.

In case you have some logging, complex initialization or need to free some resources, you have to use @BeforeClass and @AfterClass

查看更多
一纸荒年 Trace。
3楼-- · 2019-04-27 16:21

Suppose, you had all of your Food related data (say a Menu) setup at the backend in a database table. Your Food test cases could then pertain to updating the Menu (all the CRUD ops basically).

Instead of opening a DB connection for every test case (using @Before); it would be wise if you do it just once before you run all your test cases via a method marked @BeforeClass.

Now the use of a method makes sense as the setup would most probably be slightly complex (you may decide to use a Spring container to get your Connection from a DataSource) and you would not be able to achieve it with a single line where you declare your Connection object.

Similarly, you would use the @AfterClass to tear down your global setup (for all the test cases) i.e. closing your database connection here.

查看更多
Animai°情兽
4楼-- · 2019-04-27 16:25

Inheritance adds another wrinkle:

Let's say you have two JUnit tests that extend a common base class. And let's say the base class has both a static initializer block and a @BeforeClass method. In this case, the static initializer block will run once, while the @BeforeClass method will run twice.

So, if you have a very expensive computation or resource that you need set up across a whole suite of test cases that share a common base class, then you could use the static initializer block for that.

查看更多
该账号已被封号
5楼-- · 2019-04-27 16:29

I think the idea is like that: You use @AfterClass to free resources. Then it is logical to have @BeforeClass to acquire them. Because it may not be a good idea to let developer to guess that he need to use static block.

查看更多
做自己的国王
6楼-- · 2019-04-27 16:31

Almost no difference. But if constructor of Sandwich throws exception you cannot initialize it directly static private Food sandwich = new Sandwich(); but have to wrap initialization with try/catch block. However method initialise() may be declared as throws MyException, so the test case will fail if exception indeed thrown during initialization.

查看更多
Anthone
7楼-- · 2019-04-27 16:38

In your particular example - not much. However there is also @Before annotation which will run prior to every test in your class. Take a look at http://selftechy.com/2011/05/17/junit4-before-vs-beforeclass-after-vs-afterclass, it is explained well there.

查看更多
登录 后发表回答