Sub topic 11
Grouping Tests with @Nested
As a test class grows, its methods turn into one long list. Seeing which tests belong together gets hard. JUnit 5 lets you sort tests into groups with @Nested.
What @Nested Is
A nested class is a class written inside another class. @Nested is an annotation that tells JUnit to treat such an inner class as a group of tests. Each group can describe one situation, such as "when the stack is empty" or "after adding an item."
Groups help in three ways:
- Related tests sit together in the code.
- The test report shows a tree, which makes it easy to read.
- Each group can add its own setup on top of the outer setup.
A Worked Example
This example tests a stack. A stack is a collection where the last item you add is the first item you take out. Java's ArrayDeque class works as a stack.
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.NoSuchElementException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@DisplayName("A stack")
class StackTest {
Deque<String> stack;
@BeforeEach
void createStack() {
stack = new ArrayDeque<>();
}
@Nested
@DisplayName("when new")
class WhenNew {
@Test
@DisplayName("is empty")
void isEmpty() {
assertTrue(stack.isEmpty());
}
@Test
@DisplayName("throws when popped")
void popThrows() {
assertThrows(NoSuchElementException.class, () -> stack.pop());
}
}
@Nested
@DisplayName("after pushing an element")
class AfterPushing {
@BeforeEach
void pushElement() {
stack.push("apple");
}
@Test
@DisplayName("is not empty")
void isNotEmpty() {
assertFalse(stack.isEmpty());
}
@Test
@DisplayName("returns the element when popped")
void popReturnsElement() {
assertEquals("apple", stack.pop());
assertTrue(stack.isEmpty());
}
}
}When you run it, your IDE shows a tree like this:
A stack
├── when new
│ ├── is empty
│ └── throws when popped
└── after pushing an element
├── is not empty
└── returns the element when poppedThe tree reads almost like plain sentences: "A stack, when new, is empty."
How Setup Runs
Before each nested test, JUnit runs the outer @BeforeEach first and then the inner one. Each test in AfterPushing gets a fresh, empty stack and then has "apple" pushed onto it.
Inner classes can read the outer class's fields, so stack is visible inside both groups.
When to Use @Nested
| Use @Nested when... | Skip it when... |
|---|---|
| Tests share a starting situation | The class has only a few tests |
| You want the report to read like a description | Every test needs completely different setup |
| An inner group needs extra setup steps | The nesting would go more than two or three levels deep |
Exercise
Add a nested group inside AfterPushing called AfterPushingTwice. Its @BeforeEach pushes "banana". Write a test that checks pop() returns "banana". Which setup methods run before that test, and in what order?
Show answer
Three setup methods run in this order: createStack() from StackTest, then pushElement() from AfterPushing, then the new method in AfterPushingTwice that pushes "banana". The stack holds "banana" on top of "apple", so assertEquals("banana", stack.pop()) passes. Remember that AfterPushingTwice must be a non-static inner class with @Nested.
Exercise
A teammate writes "@Nested static class WhenFull" and its tests never appear in the report. Why?
Show answer
Nested test classes must be non-static inner classes. JUnit skips a static class marked @Nested. Remove the static keyword so JUnit runs the tests.