Skip to content

Commit 90ce5a6

Browse files
committed
Avoid reporting discovery warnings for abstract inner classes with tests
Resolves #4635. (cherry picked from commit aee7c88)
1 parent d7ce21b commit 90ce5a6

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 = String.format(
118120
"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.",
119121
nestedClass.getName());

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 = String.format(
@@ -421,4 +428,19 @@ void nested() {
421428
}
422429
}
423430

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

0 commit comments

Comments
 (0)