Java: refactor QL

This commit is contained in:
Jami Cogswell
2025-03-21 15:22:59 -04:00
parent ccbe77eb09
commit f17e7266cf
2 changed files with 17 additions and 17 deletions

View File

@@ -112,6 +112,14 @@ class JUnitJupiterTestMethod extends Method {
}
}
/**
* A JUnit test class that contains at least one method annotated with
* `org.junit.jupiter.api.Test`.
*/
class JUnit5TestClass extends Class {
JUnit5TestClass() { this.getAMethod() instanceof JUnitJupiterTestMethod }
}
/**
* A JUnit `@Ignore` annotation.
*/

View File

@@ -1,30 +1,22 @@
/**
* @id java/junit5-non-static-inner-class-missing-nested-annotation
* @name J-T-004: Non-static inner class defined in a JUnit5 test is missing a `@Nested` annotation
* @description A non-static inner class defined in a JUnit5 test missing a `@Nested` annotation
* @name Non-static inner class defined in a JUnit 5 test is missing a `@Nested` annotation
* @description A non-static inner class defined in a JUnit 5 test missing a `@Nested` annotation
* will be excluded from execution and it may indicate a misunderstanding from the
* programmer.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags maintainability
* @tags quality
* maintainability
* correctness
*/
import java
class JUnit5TestClass extends Class {
JUnit5TestClass() {
this.getAMethod().getAnAnnotation().getType().hasQualifiedName("org.junit.jupiter.api", "Test")
}
}
from JUnit5TestClass testClass // `TestClass` by definition should have at least one @Test method.
from JUnit5TestClass testClass
where
not testClass.isStatic() and
testClass instanceof InnerClass and // `InnerClass` is by definition a non-static nested class.
not exists(Annotation annotation |
annotation.getType().hasQualifiedName("org.junit.jupiter.api", "Nested") and
annotation.getAnnotatedElement() = testClass
)
select testClass, "This JUnit5 inner test class lacks a @Nested annotation."
// `InnerClass` is a non-static, nested class.
testClass instanceof InnerClass and
not testClass.hasAnnotation("org.junit.jupiter.api", "Nested")
select testClass, "This JUnit5 inner test class lacks a '@Nested' annotation."