Skip to content

Consider allowing static @Nested classes #4622

@vlsi

Description

@vlsi

It would be great if JUnit could support @Nested static class 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 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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions