mirror of
https://github.com/github/codeql.git
synced 2026-04-22 23:35:14 +02:00
Java: refactor QL
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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."
|
||||
|
||||
Reference in New Issue
Block a user