167

When unit testing with JUnit, there are two similar methods, setUp() and setUpBeforeClass(). What is the difference between these methods? Also, what is the difference between tearDown() and tearDownAfterClass()?

Here are the signatures:

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
}

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}
Pops
  • 30,199
  • 37
  • 136
  • 151
Sagar Varpe
  • 3,531
  • 4
  • 27
  • 43

4 Answers4

217

The @BeforeClass and @AfterClass annotated methods will be run exactly once during your test run - at the very beginning and end of the test as a whole, before anything else is run. In fact, they're run before the test class is even constructed, which is why they must be declared static.

The @Before and @After methods will be run before and after every test case, so will probably be run multiple times during a test run.

So let's assume you had three tests in your class, the order of method calls would be:

setUpBeforeClass()

  (Test class first instance constructed and the following methods called on it)
    setUp()
    test1()
    tearDown()

  (Test class second instance constructed and the following methods called on it)
    setUp()
    test2()
    tearDown()

  (Test class third instance constructed and the following methods called on it)
    setUp()
    test3()
    tearDown()

tearDownAfterClass()
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
15

Think of "BeforeClass" as a static initializer for your test case - use it for initializing static data - things that do not change across your test cases. You definitely want to be careful about static resources that are not thread safe.

Finally, use the "AfterClass" annotated method to clean up any setup you did in the "BeforeClass" annotated method (unless their self destruction is good enough).

"Before" & "After" are for unit test specific initialization. I typically use these methods to initialize / re-initialize the mocks of my dependencies. Obviously, this initialization is not specific to a unit test, but general to all unit tests.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
madhurtanwani
  • 1,199
  • 7
  • 13
  • BTW if you beginning to write unit test I would recommend this pot from my blog. It has pointers to other great material on unit testing as well : http://madhurtanwani.blogspot.com/search/label/mock – madhurtanwani Aug 05 '10 at 09:37
9

setUpBeforeClass is run only once, right after the test class constructor (before beginning test method executions)

setUp is run before each method execution

tearDown is run after each method execution

tearDownAfterClass is run only once, only after all other method executions have completed; it is the last method to be executed.

nbrooks
  • 18,126
  • 5
  • 54
  • 66
netbrain
  • 9,194
  • 6
  • 42
  • 68
5

From the Javadoc:

Sometimes several tests need to share computationally expensive setup (like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization. Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those the current class.

Pops
  • 30,199
  • 37
  • 136
  • 151
Justin King
  • 1,428
  • 12
  • 14
  • The difference being that setUpBeforeClass is run before any of the tests and is run once; setUp is run once before each test (and is usually used to reset the testing state to a known-good value between tests). – Syntax Aug 05 '10 at 08:56