1

According to this answer, the @Before annotation is executed once before each test, whereas the @BeforeClass annotation is only executed once before all tests.

My intuition tells me to always use @BeforeClass, so the question is, why even use @Before? Is there a case where the @Before annotation performs better/faster than the @BeforeClass annotation?

9
  • 1
    Perform better? Why do you want performance into your junit tests? The @Before is usefull to reset a context before each test... Commented Oct 30, 2015 at 17:24
  • 1
    For my specific case, I'm currently using @Before paired with assumeFalse to exclude certain tests. So it's not really a performance issue I guess, it's more of, if I don't need to re-check redundant conditions, I should be using @BeforeClass. Commented Oct 30, 2015 at 17:30
  • Could you provide an example of what you mean by that? Commented Oct 30, 2015 at 17:32
  • @Before public void exclude() { assumeFalse(condition); } Commented Oct 30, 2015 at 17:33
  • 1
    To excude a test you may anotate it with @Ignore, no? Commented Oct 31, 2015 at 7:52

3 Answers 3

3

Each test should be isolated. By using BeforeClass then the state of the previous test could be hanging about and messing up the later tests.

One generally uses BeforeClass to setup an expensive external resource and Before to reset the world (be that the external resource or local).

One final note:

performs better/faster

In this case I would say the two properties should be treated independently here (that is better!=faster). As long as the total time to run your complete tests suite is under 10 minutes then "faster" is simply not important.

Once you go above that magic ten minutes (and at this point you will definitely be talking integration tests not unit tests) then start looking for ways to make things "faster".

Sign up to request clarification or add additional context in comments.

Comments

3

I'll give you an example:

Say you need to validate a form in your web application. You are going to use Selenium for that.

Your test class will have 10 tests. You don't need to open and close your webdriver browser each time you start your test suite. In such case, you iniatilize your webdrive browser on a @BeforeClass method.

However, for each validation test you need to reset your form. This action you will perform on a @Before method.

This is a very simple example, but may make things clearer.

Comments

1

When you use @BeforeClass it suggests that you do initialization before all the tests. So one test might depend on other one (e.g. if tests change the state of fields), so I would encourage usage of @Before, or even better to do preparations for the test method inside the method itself. There are very rarely cases when your tests need exactly the same initial state, so why couple them together?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.