Skip to content

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

Closed
@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.

assertThatExceptionOfType(JUnitException.class)//
.isThrownBy(() -> predicates.looksLikeIntendedTestClass(TestCases.OuterClass.class))//
.withMessage("Detected cycle in inner class hierarchy between %s and %s",
TestCases.OuterClass.RecursiveInnerClass.class.getName(), TestCases.OuterClass.class.getName());

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