Skip to content

Commit aee7c88

Browse files
authored
Avoid reporting discovery warnings for abstract inner classes with tests
Resolves #4635.
1 parent 8b77838 commit aee7c88

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.13.2.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ repository on GitHub.
3737

3838
* Stop reporting discovery issues for cyclic inner class hierarchies not annotated with
3939
`@Nested`.
40+
* Stop reporting discovery issues for _abstract_ inner classes that contain test methods
41+
but are not annotated with `@Nested`.
4042

4143
[[release-notes-5.13.2-junit-jupiter-deprecations-and-breaking-changes]]
4244
==== Deprecations and Breaking Changes

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/discovery/ClassSelectorResolver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.platform.commons.support.ReflectionSupport.findMethods;
2020
import static org.junit.platform.commons.util.FunctionUtils.where;
2121
import static org.junit.platform.commons.util.ReflectionUtils.isInnerClass;
22+
import static org.junit.platform.commons.util.ReflectionUtils.isNotAbstract;
2223
import static org.junit.platform.commons.util.ReflectionUtils.streamNestedClasses;
2324
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId;
2425
import static org.junit.platform.engine.support.discovery.SelectorResolver.Resolution.unresolved;
@@ -113,7 +114,8 @@ public Resolution resolve(NestedClassSelector selector, Context context) {
113114
parent -> Optional.of(newMemberClassTestDescriptor(parent, nestedClass))));
114115
}
115116
}
116-
else if (isInnerClass(nestedClass) && predicates.looksLikeIntendedTestClass(nestedClass)) {
117+
else if (isInnerClass(nestedClass) && isNotAbstract(nestedClass)
118+
&& predicates.looksLikeIntendedTestClass(nestedClass)) {
117119
String message = "Inner class '%s' looks like it was intended to be a test class but will not be executed. It must be static or annotated with @Nested.".formatted(
118120
nestedClass.getName());
119121
issueReporter.reportIssue(DiscoveryIssue.builder(Severity.WARNING, message) //

jupiter-tests/src/test/java/org/junit/jupiter/engine/NestedTestClassesTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ void individualMethodsWithinRecursiveNestedTestClassHierarchiesAreExecuted() {
230230
executionResults.testEvents().assertStatistics(stats -> stats.started(1).succeeded(1));
231231
}
232232

233+
@Test
234+
void doesNotReportDiscoveryIssueForAbstractInnerClass() {
235+
var discoveryIssues = discoverTestsForClass(ConcreteWithExtendedInnerClassTestCase.class).getDiscoveryIssues();
236+
237+
assertThat(discoveryIssues).isEmpty();
238+
}
239+
233240
private void assertNestedCycle(Class<?> start, Class<?> from, Class<?> to) {
234241
var results = executeTestsForClass(start);
235242
var expectedMessage = "Cause: org.junit.platform.commons.JUnitException: Detected cycle in inner class hierarchy between %s and %s".formatted(
@@ -420,4 +427,19 @@ void nested() {
420427
}
421428
}
422429

430+
static class AbstractBaseWithInnerClassTestCase {
431+
@SuppressWarnings("InnerClassMayBeStatic")
432+
abstract class AbstractInnerClass {
433+
@Test
434+
void test() {
435+
}
436+
}
437+
}
438+
439+
static class ConcreteWithExtendedInnerClassTestCase extends AbstractBaseWithInnerClassTestCase {
440+
@Nested
441+
class NestedTests extends AbstractInnerClass {
442+
}
443+
}
444+
423445
}

0 commit comments

Comments
 (0)