@Nested marks an inner (non-static) class as a nested test class in JUnit 5. It lets you group related tests under a shared context, give each group its own setup, and show the groups as a readable tree in IDEs and reports.
Nested classes can access the fields and methods of the enclosing test class, and the lifecycle callbacks of the outer class run before those of the nested class. This makes @Nested the standard way to write behaviour-style tests in JUnit Jupiter.
A test class can contain inner classes annotated with @Nested. Each inner class is a full test class: it can declare @Test, @BeforeEach, @AfterEach and @DisplayName. JUnit instantiates the outer class first, then the nested class, so the nested tests see the outer state.
Typical reasons to use it:
The example tests a simple stack, grouped by state.
The output in an IDE or Gradle HTML report shows StackTest with the two nested groups below it, each using its own display name.
For one test inside AfterPushing, JUnit runs the callbacks in this order:
@BeforeEach of the outer class (createStack)@BeforeEach of the nested class (pushOne)@Test method@AfterEach methods of the nested class, then of the outer classThis is why the stack in the example is already created when pushOne runs, and why each test gets a fresh stack: the default lifecycle creates a new instance of the test class per test method.
Before Java 16, inner classes could not declare static members, so @BeforeAll and @AfterAll were not usable in a @Nested class unless you switched to @TestInstance(Lifecycle.PER_CLASS). Since Java 16, inner classes can have static members and static @BeforeAll methods work. On older language levels, use PER_CLASS on the nested class.
You can nest classes more than one level deep, but two levels are usually the limit of readability. Use @DisplayName so reports read as sentences, for example "StackTest > after pushing an element > pop returns the element".
Nested classes must be non-static inner classes. A static nested class is not discovered by @Nested; it would need to be a regular test class on its own.
@ParameterizedTest works inside nested classes.@ExtendWith also applies to nested classes.@Tag on the outer class is inherited by the nested classes, which helps filter whole groups.@SpringBootTest on the outer class is inherited, so nested classes share the same cached application context. Placing context configuration on the outer class avoids extra context startups.@Disabled on the nested class to skip every test inside it.static. It will not run as a nested test.@BeforeEach or a shared extension.No. @Nested is part of JUnit Jupiter (JUnit 5 and later). In JUnit 4 you would use the Enclosed runner with static nested classes.
Yes. They are inner classes, so they see all fields and methods of the enclosing test class, including private ones.
Yes on Java 16 and later with a static method, or with @TestInstance(Lifecycle.PER_CLASS) on older versions.
Two or three. Beyond that, split the tests into separate classes.
@Nested groups tests inside inner classes so that each group has its own context and setup while inheriting the outer class's state and callbacks. Use it with @DisplayName for readable reports, keep it to a couple of levels, and remember that nested classes must not be static.