Skip to content

Commit ed57bc7

Browse files
Jami CogswellJami Cogswell
authored andcommitted
Java: exclude abstract classes
1 parent b08c8d0 commit ed57bc7

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

java/ql/src/Likely Bugs/Frameworks/JUnit/JUnit5NonStaticInnerClassMissingNestedAnnotation.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,18 @@ public class IntegerOperationTest {
4747

4848
## Implementation Notes
4949

50-
The `@Nested` annotation does not apply to inner static classes, since the meaning of the annotation is to mark a class as "a *non-static* inner class containing `@Test` methods to be picked up by a build system". Therefore, this rule does not aim to target static inner test classes with a `@Nested` annotation, nor does it try to enforce such correct usage of `@Nested`. Therefore, any code that resembles below is not non-compliant to this rule.
50+
The `@Nested` annotation does not apply to inner static classes, since the meaning of the annotation is to mark a class as "a *non-static* inner class containing `@Test` methods to be picked up by a build system". It also does not apply to inner abstract classes since there is no use case for an `@Nested` annotation on an abstract class. Therefore, this rule does not aim to target static or abstract inner test classes with a `@Nested` annotation, nor does it try to enforce such correct usage of `@Nested`. Therefore, any code that resembles the below is not non-compliant to this rule.
5151

5252
``` java
5353
@Nested
54-
public static class Test6 { // COMPLIANT: Although invalid, this matter is out of the scope
54+
public static class TestStatic { // COMPLIANT: Although invalid, this matter is out of the scope
55+
@Test
56+
public void test() {
57+
}
58+
}
59+
60+
@Nested
61+
public abstract class TestAbstract { // COMPLIANT: Although invalid, this matter is out of the scope
5562
@Test
5663
public void test() {
5764
}

java/ql/src/Likely Bugs/Frameworks/JUnit/JUnit5NonStaticInnerClassMissingNestedAnnotation.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ from JUnit5TestClass testClass
1818
where
1919
// `InnerClass` is a non-static, nested class.
2020
testClass instanceof InnerClass and
21-
not testClass.hasAnnotation("org.junit.jupiter.api", "Nested")
21+
not testClass.hasAnnotation("org.junit.jupiter.api", "Nested") and
22+
// An abstract class should not have a `@Nested` annotation
23+
not testClass.isAbstract()
2224
select testClass, "This JUnit5 inner test class lacks a '@Nested' annotation."

java/ql/test/query-tests/Likely Bugs/Frameworks/JUnit/AnnotationTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,19 @@ public static class Test6 {
4444
public void test() {
4545
}
4646
}
47+
48+
public abstract class Test7 { // COMPLIANT: Abstract inner test classes don't need `@Nested`
49+
@Test
50+
public void test() {
51+
}
52+
}
53+
54+
// COMPLIANT: Invalid to use `@Nested` on an abstract class, but
55+
// this matter is out of scope (see QHelp Implementation Notes)
56+
@Nested
57+
public abstract class Test8 {
58+
@Test
59+
public void test() {
60+
}
61+
}
4762
}

0 commit comments

Comments
 (0)