Skip to content

Commit f17e726

Browse files
Jami CogswellJami Cogswell
authored andcommitted
Java: refactor QL
1 parent ccbe77e commit f17e726

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

java/ql/lib/semmle/code/java/UnitTests.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ class JUnitJupiterTestMethod extends Method {
112112
}
113113
}
114114

115+
/**
116+
* A JUnit test class that contains at least one method annotated with
117+
* `org.junit.jupiter.api.Test`.
118+
*/
119+
class JUnit5TestClass extends Class {
120+
JUnit5TestClass() { this.getAMethod() instanceof JUnitJupiterTestMethod }
121+
}
122+
115123
/**
116124
* A JUnit `@Ignore` annotation.
117125
*/
Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
/**
22
* @id java/junit5-non-static-inner-class-missing-nested-annotation
3-
* @name J-T-004: Non-static inner class defined in a JUnit5 test is missing a `@Nested` annotation
4-
* @description A non-static inner class defined in a JUnit5 test missing a `@Nested` annotation
3+
* @name Non-static inner class defined in a JUnit 5 test is missing a `@Nested` annotation
4+
* @description A non-static inner class defined in a JUnit 5 test missing a `@Nested` annotation
55
* will be excluded from execution and it may indicate a misunderstanding from the
66
* programmer.
77
* @kind problem
88
* @precision very-high
99
* @problem.severity warning
10-
* @tags maintainability
10+
* @tags quality
11+
* maintainability
1112
* correctness
1213
*/
1314

1415
import java
1516

16-
class JUnit5TestClass extends Class {
17-
JUnit5TestClass() {
18-
this.getAMethod().getAnAnnotation().getType().hasQualifiedName("org.junit.jupiter.api", "Test")
19-
}
20-
}
21-
22-
from JUnit5TestClass testClass // `TestClass` by definition should have at least one @Test method.
17+
from JUnit5TestClass testClass
2318
where
24-
not testClass.isStatic() and
25-
testClass instanceof InnerClass and // `InnerClass` is by definition a non-static nested class.
26-
not exists(Annotation annotation |
27-
annotation.getType().hasQualifiedName("org.junit.jupiter.api", "Nested") and
28-
annotation.getAnnotatedElement() = testClass
29-
)
30-
select testClass, "This JUnit5 inner test class lacks a @Nested annotation."
19+
// `InnerClass` is a non-static, nested class.
20+
testClass instanceof InnerClass and
21+
not testClass.hasAnnotation("org.junit.jupiter.api", "Nested")
22+
select testClass, "This JUnit5 inner test class lacks a '@Nested' annotation."

0 commit comments

Comments
 (0)