Sub topic 8
Setup and Teardown with Lifecycle Methods
Many tests need the same starting objects. You could build them again inside every test, but then the same lines repeat everywhere. JUnit gives you lifecycle methods for this. A lifecycle method is a method that JUnit runs automatically before or after your tests.
What a fixture is
A fixture is the starting state a test needs, such as a list with some items or a calculator object. Setup is the work of building that fixture. Teardown is the cleanup that runs afterwards.
Every test should get a fresh fixture. If one test changes the fixture, the next test should not see that change. This keeps your tests independent, which means each test passes or fails without depending on any other test.
The four lifecycle annotations
| Annotation | When it runs | Must be static? |
|---|---|---|
| `@BeforeEach` | Before every test method | No |
| `@AfterEach` | After every test method | No |
| `@BeforeAll` | Once, before all tests in the class | Yes |
| `@AfterAll` | Once, after all tests in the class | Yes |
Use @BeforeEach and @AfterEach most of the time. Save @BeforeAll and @AfterAll for setup that is slow and safe to share, such as loading a large file that no test changes.
A worked example
This class prints a message from each lifecycle method, so you can see the order they run in.
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class ShoppingListLifecycleTest {
private List<String> items;
@BeforeAll
static void startAll() {
System.out.println("BeforeAll: runs once");
}
@BeforeEach
void setUp() {
System.out.println("BeforeEach: fresh list");
items = new ArrayList<>();
items.add("milk");
}
@Test
void addingAnItemGrowsTheList() {
items.add("bread");
assertEquals(2, items.size());
}
@Test
void newListStartsWithMilk() {
assertTrue(items.contains("milk"));
assertEquals(1, items.size());
}
@AfterEach
void tearDown() {
System.out.println("AfterEach: clean up");
items.clear();
}
@AfterAll
static void finishAll() {
System.out.println("AfterAll: runs once");
}
}Both tests pass. The first test adds "bread" to the list. The second test still sees only one item, because setUp() built a new list before it ran.
The order of events
JUnit also creates a new instance of the test class for every test method. That is another reason fields never leak between tests.
Common mistakes
- Sharing changeable state in
@BeforeAll. If one test changes a shared object, later tests can fail for confusing reasons. Build changeable objects in@BeforeEach. - Putting assertions in setup. Setup should build the fixture. The checking belongs in the tests.
- Skipping cleanup. If a test opens a file or a connection, close it in
@AfterEachso it does not affect the next test.
Exercise
A test class has three @Test methods, one @BeforeEach method, and one @BeforeAll method. How many times does each lifecycle method run?
Show answer
The @BeforeEach method runs three times, once before each test. The @BeforeAll method runs once, before any test starts.
Exercise
You write a @BeforeAll method called loadData() without the static keyword, using JUnit's default settings. What happens, and how do you fix it?
Show answer
JUnit reports an error and does not run the tests in that class, because @BeforeAll methods must be static by default. Fix it by declaring the method as "static void loadData()".