You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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
-
5
1
## Overview
6
2
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.
10
4
11
5
## Recommendation
12
6
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`.
privateint i; // Shared variable among the inner classes.
16
+
privateint i; // Shared variable among the inner classes.
23
17
24
18
@BeforeEach
25
19
publicvoidinitTest() { i =0; }
26
20
27
21
@Nested
28
-
publicclassAdditionTest { // COMPLIANT: Inner test class annotated with `@Nested`.
22
+
publicclassAdditionTest { // COMPLIANT: Inner test class annotated with `@Nested`.
29
23
@Test
30
-
publicvoidaddTest1() {// Test of an inner class, implying `AdditionTest` is a test class.
31
-
assertEquals(1, i+1);
24
+
publicvoidaddTest1() {
25
+
assertEquals(1, i+1);
32
26
}
33
27
}
34
28
35
-
publicclassSubtractionTest { // NON_COMPLIANT: Inner test class missing `@Nested`.
29
+
publicclassSubtractionTest { // NON_COMPLIANT: Inner test class missing `@Nested`.
36
30
@Test
37
-
publicvoidaddTest1() {// Test of an inner class, implying `SubtractionTest` is a test class.
38
-
assertEquals(-1, i-1);
31
+
publicvoidaddTest1() {
32
+
assertEquals(-1, i-1);
39
33
}
40
34
}
41
-
42
-
staticpublicclassMultiplicationTest { // COMPLIANT: static test class should not be annotated as `@Nested`.
43
-
...
44
-
}
45
35
}
46
36
```
47
37
48
38
## Implementation Notes
49
39
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
-
publicstaticclassTestStatic { // COMPLIANT: Although invalid, this matter is out of the scope
55
-
@Test
56
-
publicvoidtest() {
57
-
}
58
-
}
59
-
60
-
@Nested
61
-
publicabstractclassTestAbstract { // COMPLIANT: Although invalid, this matter is out of the scope
62
-
@Test
63
-
publicvoidtest() {
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.
67
41
68
42
## References
69
43
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).
* 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.
0 commit comments