Java: exclude anonymous, local, and private classes

This commit is contained in:
Jami Cogswell
2025-03-23 14:31:11 -04:00
parent 3e13f0ed41
commit 4d7bed6181
3 changed files with 43 additions and 3 deletions

View File

@@ -120,6 +120,20 @@ class JUnit5TestClass extends Class {
JUnit5TestClass() { this.getAMethod() instanceof JUnitJupiterTestMethod }
}
/**
* A JUnit inner test class that is non-anonymous, non-local,
* and non-private.
*/
class JUnit5InnerTestClass extends JUnit5TestClass {
JUnit5InnerTestClass() {
// `InnerClass` is a non-static nested class.
this instanceof InnerClass and
not this.isAnonymous() and
not this.isLocal() and
not this.isPrivate()
}
}
/**
* A JUnit `@Ignore` annotation.
*/

View File

@@ -15,10 +15,8 @@
import java
from JUnit5TestClass testClass
from JUnit5InnerTestClass testClass
where
// `InnerClass` is a non-static, nested class.
testClass instanceof InnerClass and
not testClass.hasAnnotation("org.junit.jupiter.api", "Nested") and
// An abstract class should not have a `@Nested` annotation
not testClass.isAbstract()

View File

@@ -59,4 +59,32 @@ public class AnnotationTest {
public void test() {
}
}
interface Test9 {
}
public void f() {
// COMPLIANT: anonymous classes are not considered as inner test
// classes by JUnit and therefore don't need `@Nested`
new Test9() {
@Test
public void test() {
}
};
// COMPLIANT: local classes are not considered as inner test
// classes by JUnit and therefore don't need `@Nested`
class Test10 {
@Test
void test() {
}
}
}
// COMPLIANT: private classes are not considered as inner test
// classes by JUnit and therefore don't need `@Nested`
private class Test11 {
@Test
public void test() {
}
}
}