Skip to content

Consider supporting static @Nested test classes #4622

Description

@vlsi

It would be great if JUnit could support @Nested static classes.

Currently, only inner classes (non-static nested classes) are supported for @Nested execution.

It has several drawbacks:

  • static fields, methods, and interfaces are not allowed inside inner classes in Java 8. It causes issues if users want their test code to be executable with Java 8. For the time being, https://github.com/pgjdbc/pgjdbc uses both JUnit 5, and it supports a Java 8 runtime.
  • Testcontainers-java has quite a few JUnit4 Enclosed tests which use static nested classes, and it would be great if JUnit could allow upgrading without rewriting the tests to non-static. At least, it is not clear why non-static would be better there.
  • inner classes might accidentally access the scope of the outer class, so it makes sense making the class static unless it explicitly wants accessing the state of the outer instance.

See also:

Edge cases:

  • As per @marcphilipp's comment, currently JUnit treats static nested classes as if they were just regular top-level classes. It might be if JUnit allows @Nested static class, the discovery should be updated to prevent the class to be treated as both top-level and nested.

Activity

  1. added theissue type on Jun 5, 2025
  2. added this to the 6.2.0-M1 milestone on May 22, 2026
  3. changed the title [-]Consider allowing static @Nested classes[/-] [+]Consider supporting static `@Nested` test classes[/+] on Jun 3, 2026
  4. modified the milestones: 6.2.0-M1, 6.2.0-M2 on Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions