Skip to content

Commit afa2464

Browse files
Jami CogswellJami Cogswell
authored andcommitted
Java: remove redundant 'non-static' wording and update qhelp
1 parent e7d2492 commit afa2464

File tree

5 files changed

+22
-47
lines changed

5 files changed

+22
-47
lines changed
Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
# J-T-004: Non-static inner class defined in a JUnit 5 is missing a `@Nested` annotation
2-
3-
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.
4-
51
## Overview
62

7-
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 the inner class is not annotated with `@Nested`, the unit tests in it will fail to execute during builds.
8-
9-
Note that static classes, whether static or not, behave as independent sets of unit tests. Thus, inner static classes do not share the information provided by the outer class with other inner classes. Thus, this rule only applies to non-static JUnit 5 inner classes.
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.
104

115
## Recommendation
126

13-
If you want the tests defined in an inner class to be recognized by the build plugin and be executed, annotate with `@Nested`, imported from `org.junit.jupiter.api`.
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`.
148

159
## Example
1610

@@ -19,53 +13,33 @@ import org.junit.jupiter.api.Nested;
1913
import static org.junit.Assert.assertEquals;
2014

2115
public class IntegerOperationTest {
22-
private int i; // Shared variable among the inner classes.
16+
private int i; // Shared variable among the inner classes.
2317

2418
@BeforeEach
2519
public void initTest() { i = 0; }
2620

2721
@Nested
28-
public class AdditionTest { // COMPLIANT: Inner test class annotated with `@Nested`.
22+
public class AdditionTest { // COMPLIANT: Inner test class annotated with `@Nested`.
2923
@Test
30-
public void addTest1() { // Test of an inner class, implying `AdditionTest` is a test class.
31-
assertEquals(1, i+1);
24+
public void addTest1() {
25+
assertEquals(1, i + 1);
3226
}
3327
}
3428

35-
public class SubtractionTest { // NON_COMPLIANT: Inner test class missing `@Nested`.
29+
public class SubtractionTest { // NON_COMPLIANT: Inner test class missing `@Nested`.
3630
@Test
37-
public void addTest1() { // Test of an inner class, implying `SubtractionTest` is a test class.
38-
assertEquals(-1, i-1);
31+
public void addTest1() {
32+
assertEquals(-1, i - 1);
3933
}
4034
}
41-
42-
static public class MultiplicationTest { // COMPLIANT: static test class should not be annotated as `@Nested`.
43-
...
44-
}
4535
}
4636
```
4737

4838
## Implementation Notes
4939

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.
51-
52-
``` java
53-
@Nested
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
62-
@Test
63-
public void test() {
64-
}
65-
}
66-
```
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.
6741

6842
## References
6943

70-
- JUnit: [Documentation on `@Nested`](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/Nested.html).
71-
- Baeldung: [JUnit 5 @Nested Test Classes](https://www.baeldung.com/junit-5-nested-test-classes).
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).
Lines changed: 6 additions & 5 deletions
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+
* prior-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."
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| AnnotationTest.java:13:16:13:20 | Test2 | This JUnit5 inner test class lacks a '@Nested' annotation. |
1+
| AnnotationTest.java:13:16:13:20 | Test2 | This JUnit 5 inner test class lacks a '@Nested' annotation. |

0 commit comments

Comments
 (0)