JUnit 5 nested tests use @Nested inner classes to group related tests, and tags use @Tag to label tests so you can include or exclude them at run time. Together with @DisplayName, they turn a flat list of test methods into a readable, filterable specification of how a class behaves.
This guide covers the keywords junit 5 nested tests tags in practice: how to structure a nested test class, how lifecycle callbacks behave, and how to run only the tagged tests from Maven or Gradle.
A @Nested class is a non-static inner class of your test class. It lets you describe one situation per group, such as "when the stack is empty" and "after pushing an element", and keep the shared setup for that situation in one place.
The test report shows a tree: "A stack", then "when new", then "after pushing an element". Each leaf reads like a sentence. This is the "describe/context" style familiar from behaviour-driven testing.
Callbacks run from the outside in, then back out:
@BeforeEach methods run first.@BeforeEach methods of the nested class, and then of any deeper nested class.@AfterEach methods run in the reverse order, innermost first.In the stack example, create() runs before push(), so by the time AfterPush tests execute the stack exists and holds one element. A new instance of each test class is created for every test method, so state set in one test never leaks into another.
Two limits matter. First, nested classes must be non-static inner classes, so a static class will not be discovered by @Nested. Second, @BeforeAll and @AfterAll need static methods and an inner class could not declare those before Java 16. On Java 16 and later static members in inner classes are allowed, and with @TestInstance(Lifecycle.PER_CLASS) the methods can be non-static too.
@DisplayName replaces the class or method name in reports and IDEs. It accepts spaces, punctuation and Unicode, so you can write a full phrase. Use it sparingly: a descriptive method name is often enough, and a display name that is out of date with the code is worse than none.
If you prefer to avoid writing a name on every method, @DisplayNameGeneration can derive names automatically. The built-in ReplaceUnderscores generator turns returns_the_element_when_popped into plain words:
@Tag("name") attaches a label to a test method or class. A tag is a plain string with no spaces, commas, parentheses or leading or trailing whitespace, and it must not use reserved characters such as !, & or |. Typical tags describe the cost or purpose of a test:
A tag on a class applies to every test in it, including nested classes. You can also create a composed annotation so the team uses one consistent label:
Then @IntegrationTest on a method is the same as @Tag("integration"), and a typo is a compile error instead of a silently unmatched tag.
Filtering is configured in the build tool, not in code. With Maven Surefire:
With Gradle:
Tag expressions support &, | and !, for example fast & !flaky, in the tools that accept an expression. A common layout is a fast unit task on every commit and a separate integration task in the pipeline that runs integration-tagged tests.
Yes, to any depth. Keep it to two or three levels, or the setup becomes harder to follow than flat tests.
A tag on the outer class is inherited by the nested tests. A tag on a nested class applies only to that class and its contents.
The usual causes are a static modifier, a private class, or a class that is not an inner class of the test class. The class must be non-static.
@Tag the same as JUnit 4 categories?It plays the same role. @Tag replaced @Category and is simpler: it is just a string, filtered by the build tool.
Use @Nested to group tests by situation, with each level adding its own setup through @BeforeEach. Name groups and tests with @DisplayName or a display name generator so reports read like a specification. Add @Tag labels such as fast and integration, and filter by them in Maven or Gradle so each pipeline stage runs the right tests.