Skip to content

Commit 1e0c4c1

Browse files
committed
Avoid reporting discovery warnings for abstract inner classes with tests
Resolves #4635.
1 parent 1812df4 commit 1e0c4c1

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ on GitHub.
3535
[[release-notes-5.13.2-junit-jupiter-bug-fixes]]
3636
==== Bug Fixes
3737

38-
* ❓
38+
* Stop reporting discovery issues for _abstract_ inner classes that contain test methods
39+
but are not annotated with `@Nested`.
3940

4041
[[release-notes-5.13.2-junit-jupiter-deprecations-and-breaking-changes]]
4142
==== 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
@@ -20,6 +20,7 @@
2020
import static org.junit.platform.commons.support.ReflectionSupport.streamNestedClasses;
2121
import static org.junit.platform.commons.util.FunctionUtils.where;
2222
import static org.junit.platform.commons.util.ReflectionUtils.isInnerClass;
23+
import static org.junit.platform.commons.util.ReflectionUtils.isNotAbstract;
2324
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId;
2425
import static org.junit.platform.engine.support.discovery.SelectorResolver.Resolution.unresolved;
2526

@@ -112,7 +113,8 @@ public Resolution resolve(NestedClassSelector selector, Context context) {
112113
parent -> Optional.of(newMemberClassTestDescriptor(parent, nestedClass))));
113114
}
114115
}
115-
else if (isInnerClass(nestedClass) && predicates.looksLikeIntendedTestClass(nestedClass)) {
116+
else if (isInnerClass(nestedClass) && isNotAbstract(nestedClass)
117+
&& predicates.looksLikeIntendedTestClass(nestedClass)) {
116118
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(
117119
nestedClass.getName());
118120
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)