Skip to content

TestClassPredicates.looksLikeIntendedTestClass() does not consistently detect nested class hierarchy cycles #4597

@sbrannen

Description

@sbrannen

Overview

TestClassPredicatesTests.StandaloneTestClasses.recursiveHierarchies() currently passes when run with the Gradle build or in IntelliJ IDEA; however, it fails in Eclipse IDE.

Specifically, the assertion on the first line of that test method does not throw an exception in Eclipse.

https://github.com/junit-team/junit5/blob/3c5b457564d559fb0f396d7f303f8effe291daa3/jupiter-tests/src/test/java/org/junit/jupiter/engine/discovery/predicates/TestClassPredicatesTests.java#L221-L224

The reason is twofold:

  1. ReflectionUtils.visitNestedClasses(...) short-circuits the search algorithm once a @Nested test class has been detected.
  2. The order in which nested classes are returned from java.lang.Class.getDeclaredClasses() apparently differs between the OpenJDK/IntelliJ compilers and the Eclipse compiler.

The result is that ReflectionUtils.detectInnerClassCycle() only gets invoked on nested classes until the first @Nested class is encountered (or until there are no more nested classes to inspect).

Thus, if a @Nested class is detected before a nested class with a class hierarchy cycle, the cycle is not detected/reported.

In Eclipse, if you rename TestClassPredicatesTests.TestCases.OuterClass.InnerClass to TestClassPredicatesTests.TestCases.OuterClass.XXXInnerClass, the test then passes because TestClassPredicatesTests.TestCases.OuterClass.RecursiveInnerClass is encountered first, resulting in the expected JUnitException stating that it "detected [a] cycle in [the] inner class hierarchy".

Related Issues

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions