Open
Description
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, andinterfaces
are not allowed insideinner
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 usestatic
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 classstatic
unless it explicitly wants accessing the state of the outer instance
See also:
- https://errorprone.info/bugpattern/ClassCanBeStatic
- Can @Nested class be non-static? #4613
- https://github.com/testcontainers/testcontainers-java/blob/63189c8678f81c8ef4c431e4468ee74640682654/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java#L78-L91
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.