Skip to content

Commit 3e13f0e

Browse files
Jami CogswellJami Cogswell
authored andcommitted
Java: remove redundant 'non-static' wording and update qhelp
1 parent 640096c commit 3e13f0e

9 files changed

+57
-82
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Overview
2+
3+
JUnit tests are grouped in a class, and starting from JUnit 5 users can group the test classes in a bigger class so they can share the local environment of the enclosing class. While this helps to organize the unit tests and foster code reuse, if an inner test class is not annotated with `@Nested`, the unit tests in it will fail to execute during builds.
4+
5+
## Recommendation
6+
7+
If you want the tests defined in an inner class to be recognized by the build plugin and be executed, annotate the class with `@Nested`, imported from `org.junit.jupiter.api`.
8+
9+
## Example
10+
11+
```java
12+
import org.junit.jupiter.api.Nested;
13+
import static org.junit.Assert.assertEquals;
14+
15+
public class IntegerOperationTest {
16+
private int i; // Shared variable among the inner classes.
17+
18+
@BeforeEach
19+
public void initTest() { i = 0; }
20+
21+
@Nested
22+
public class AdditionTest { // COMPLIANT: Inner test class annotated with `@Nested`.
23+
@Test
24+
public void addTest1() {
25+
assertEquals(1, i + 1);
26+
}
27+
}
28+
29+
public class SubtractionTest { // NON_COMPLIANT: Inner test class missing `@Nested`.
30+
@Test
31+
public void addTest1() {
32+
assertEquals(-1, i - 1);
33+
}
34+
}
35+
}
36+
```
37+
38+
## Implementation Notes
39+
40+
This rule is focused on missing `@Nested` annotations on non-static nested (inner) test classes. Static nested test classes and abstract nested test classes should not be annotated with `@Nested`. As a result, the absence of a `@Nested` annotation on such classes is compliant. Identifying incorrect application of a `@Nested` annotation to static and abstract classes is out of scope for this rule.
41+
42+
## References
43+
44+
- JUnit 5 API Documentation: [Annotation Interface Nested](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/Nested.html).
45+
- JUnit 5 User Guide: [Nested Tests](https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested).
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
/**
2-
* @id java/junit5-non-static-inner-class-missing-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
5-
* will be excluded from execution and it may indicate a misunderstanding from the
2+
* @id java/junit5-missing-nested-annotation
3+
* @name Missing `@Nested` annotation on JUnit 5 inner test class
4+
* @description A JUnit 5 inner test class that is missing a `@Nested` annotation will be
5+
* excluded from execution and it may indicate a misunderstanding from the
66
* programmer.
77
* @kind problem
88
* @precision very-high
99
* @problem.severity warning
1010
* @tags quality
1111
* maintainability
1212
* correctness
13+
* previous-id:java/junit5-non-static-inner-class-missing-nested-annotation
1314
*/
1415

1516
import java
@@ -21,4 +22,4 @@ where
2122
not testClass.hasAnnotation("org.junit.jupiter.api", "Nested") and
2223
// An abstract class should not have a `@Nested` annotation
2324
not testClass.isAbstract()
24-
select testClass, "This JUnit5 inner test class lacks a '@Nested' annotation."
25+
select testClass, "This JUnit 5 inner test class lacks a '@Nested' annotation."

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

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
22
category: newQuery
33
---
4-
* Added a new quality query, `java/junit5-non-static-inner-class-missing-nested-annotation`, to detect missing `@Nested` annotations on non-static, inner JUnit 5 test classes.
4+
* Added a new quality query, `java/junit5-missing-nested-annotation`, to detect missing `@Nested` annotations on JUnit 5 inner test classes.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void h() {
3030
}
3131
}
3232

33-
public static class Test5 { // COMPLIANT: Static inner test classes don't need `@Nested`
33+
public static class Test5 { // COMPLIANT: Static nested test classes don't need `@Nested`
3434
@Test
3535
public void test() {
3636
}
@@ -45,7 +45,7 @@ public void test() {
4545
}
4646
}
4747

48-
public abstract class Test7 { // COMPLIANT: Abstract inner test classes don't need `@Nested`
48+
public abstract class Test7 { // COMPLIANT: Abstract nested test classes don't need `@Nested`
4949
@Test
5050
public void test() {
5151
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| AnnotationTest.java:13:16:13:20 | Test2 | This JUnit 5 inner test class lacks a '@Nested' annotation. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Likely Bugs/Frameworks/JUnit/JUnit5MissingNestedAnnotation.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql

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

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)