@Nested JUnit 5 Tests | Java Fresher Interview
Fast recall
Quick revision
Core answer
WhenAccountIsEmpty group under BankAccountTest. Each nested class can have its own @BeforeEach, and outer @BeforeEach methods run before the nested ones, so you build context incrementally. It groups scenarios for the same unit; use separate top-level classes for genuinely different units.Interview-ready guide
Interview answer
@Nested marks a non-static inner test class and lets one test class read as a hierarchy of scenarios. For example, BankAccountTest can contain WhenEmpty and WhenFunded groups, so a failure report shows both the unit and its state.
Lifecycle setup is layered: an outer @BeforeEach runs before the nested class's @BeforeEach. The outer hook can create an account and the inner hook can move it into the funded state. That avoids repeating the common setup while keeping each scenario clear.
A nested test class itself must be non-static. Class-level callbacks inside it need the same lifecycle rules as elsewhere: an instance @BeforeAll needs @TestInstance(PER_CLASS); on Java versions that allow static members in inner classes, a static callback can also be used. Keep nesting shallow enough that the setup remains easy to trace.
@Nested JUnit 5 Tests | Java Fresher Interview
Direct Answer
@Nested lets you put related tests inside an inner, non-static class so the report reads like an outline — e.g. a WhenAccountIsEmpty group under BankAccountTest. Each nested class can have its own @BeforeEach, and outer @BeforeEach methods run before the nested ones, so you build context incrementally. It groups scenarios for the same unit; use separate top-level classes for genuinely different units.
Code Example
class BankAccountTest {
BankAccount account;
@BeforeEach void createAccount() { account = new BankAccount(); } // runs first
@Nested
@DisplayName("when the account is empty")
class WhenEmpty {
@Test void balanceIsZero() { assertEquals(0, account.balance()); }
@Test void withdrawThrows() {
assertThrows(InsufficientFundsException.class, () -> account.withdraw(10));
}
}
@Nested
@DisplayName("when the account has 100")
class WhenFunded {
@BeforeEach void deposit() { account.deposit(100); } // runs AFTER createAccount
@Test void canWithdrawWithinBalance() {
account.withdraw(40);
assertEquals(60, account.balance());
}
}
}
Common Mistakes & Pitfalls
- Making the nested class
static.@Nestedrequires a non-static inner class so it can access the outer instance;staticbreaks it. - Expecting
@BeforeAllinside a nested class. It needs@TestInstance(PER_CLASS)because@BeforeAllis static by default. - Over-nesting. Three or four levels deep hurts readability; two levels is usually plenty.
- Using
@Nestedfor unrelated units. Different classes under test belong in different top-level test classes.
Check your understanding
Answer these without looking back at the article.
- Can @Nested classes have @BeforeAll methods?
- How many levels of nesting is practical in test classes?
- What is the difference between @Nested and separate test classes?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.