mirror of
https://github.com/github/codeql.git
synced 2026-04-23 15:55:18 +02:00
Java: exclude anonymous, local, and private classes
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user