@Nested Tests JUnit 5 | Java Fresher Interview
Fast recall
Quick revision
Core answer
WhenEmpty, WhenFunded). Outer @BeforeEach runs before the nested class's @BeforeEach, letting you layer setup from general to specific. It improves readability and makes failures self-locating.Interview-ready guide
Interview answer
@Nested marks a non-static inner class as a group of related tests. A bank-account test can have WhenEmpty and WhenFunded groups, so the report shows the state in which a failure occurred.
Setup is layered. The outer @BeforeEach runs before the nested class's @BeforeEach, allowing common setup first and scenario-specific setup second. For example, the outer hook creates a new account and the funded group's hook deposits money.
The nested test class itself must be non-static. An instance @BeforeAll inside it needs @TestInstance(PER_CLASS); on Java versions that permit static members in inner classes, a static callback is also possible. Use enough nesting to clarify scenarios, but stop when the setup becomes hard to trace.
@Nested Tests JUnit 5 | Java Fresher Interview
Direct Answer
@Nested groups related tests in an inner, non-static class so the report reads like an outline — grouping a unit's tests by state or scenario (e.g. WhenEmpty, WhenFunded). Outer @BeforeEach runs before the nested class's @BeforeEach, letting you layer setup from general to specific. It improves readability and makes failures self-locating.
Deep Dive & Architecture
@Nested turns a non-static inner class into a group of tests within the outer test class. It is meant for related states of the same unit, so both the source and the test report can read from general context to specific behavior.
For example, BankAccountTest can contain WhenEmpty and WhenFunded nested classes. The outer @BeforeEach creates a fresh account, then the funded group’s own @BeforeEach deposits money before each test in that group. Outer setup runs before inner setup, allowing context to be layered without copy-and-paste. Avoid deep nesting and do not use nested classes to combine unrelated production classes; separate units still belong in separate top-level test classes.
Code Example
@DisplayName("BankAccount")
class BankAccountTest {
BankAccount account;
@BeforeEach void create() { account = new BankAccount(); } // runs first
@Nested
@DisplayName("when empty")
class WhenEmpty {
@Test void balanceIsZero() { assertEquals(0, account.balance()); }
}
@Nested
@DisplayName("when funded with 100")
class WhenFunded {
@BeforeEach void deposit() { account.deposit(100); } // runs AFTER create()
@Test void allowsWithdrawal() {
account.withdraw(40);
assertEquals(60, account.balance());
}
}
}
// Report: BankAccount > when funded with 100 > allowsWithdrawal
Common Mistakes & Pitfalls
- Static nested class. A Jupiter
@Nestedtest class must be non-static. - Instance
@BeforeAllwithoutPER_CLASS. Add@TestInstance(PER_CLASS)when the callback needs the nested instance; a static callback is another option where the Java language level permits it. - Over-nesting. Deep layers make setup and reports harder to follow.
- Grouping unrelated units. Different production units normally belong in separate top-level test classes.
Check your understanding
Answer these without looking back at the article.
- What is the purpose of @BeforeEach and @AfterEach in JUnit 5?
- What does @DisplayName do in JUnit 5 and why should you use it?
- What is @ExtendWith in JUnit 5 and when do you use it?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.