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-003: Accessing any method, field or class annotated with `@VisibleForTesting` from production code is discouraged
1
+
## Overview
2
2
3
3
Accessing class members annotated with `@VisibleForTesting` from production code goes against the intention of the annotation and may indicate programmer error.
4
4
5
-
## Overview
6
-
7
-
The `@VisibleForTesting` serves to increase visibility of methods, fields or classes for the purposes of testing. Accessing methods, fields or classes that are annotated with `@VisibleForTesting` in production code (not test code) abuses the intention of the annotation.
5
+
The `@VisibleForTesting` annotation serves to increase visibility of methods, fields or classes for the purposes of testing. Accessing these annotated elements in production code (not test code) abuses the intention of the annotation.
8
6
9
7
## Recommendation
10
8
@@ -14,15 +12,14 @@ Only access methods, fields or classes annotated with `@VisibleForTesting` from
14
12
15
13
```java
16
14
publicclassAnnotated {
17
-
@VisibleForTestingstaticintf(){}
15
+
@VisibleForTestingstaticintf() { return42; }
18
16
}
19
17
20
18
/* src/test/java/Test.java */
21
19
int i =Annotated.f(); // COMPLIANT
22
20
23
21
/* src/main/Source.java */
24
-
int i =Annotated.f(); // NON_COMPLIANT
25
-
22
+
int i =Annotated.f(); // NON_COMPLIANT
26
23
```
27
24
28
25
## Implementation notes
@@ -31,9 +28,9 @@ This rule alerts on any implementation of the annotation `VisibleForTesting`, re
31
28
32
29
The rule also uses the following logic to determine what an abuse of the annotation is:
33
30
34
-
1) If public or protected member/type is annotated with `VisibleForTesting`, it's assumed that package-private access is enough for production code. Therefore the rule alerts when a public or protected member/type annotated with `VisibleForTesting` is used outside of its declaring package.
35
-
2) If package-private member/type is annotated with `VisibleForTesting`, it's assumed that private access is enough for production code. Therefore the rule alerts when a package-private member/type annotated with `VisibleForTesting` is used outside its declaring class.
31
+
1. If a public or protected member/type is annotated with `@VisibleForTesting`, it's assumed that package-private access is enough for production code. Therefore the rule alerts when a public or protected member/type annotated with `@VisibleForTesting` is used outside of its declaring package.
32
+
2. If a package-private member/type is annotated with `@VisibleForTesting`, it's assumed that private access is enough for production code. Therefore the rule alerts when a package-private member/type annotated with `@VisibleForTesting` is used outside its declaring class.
36
33
37
34
## References
38
-
-Example Specific Implementation of a VisibleForTesting Annotation: [AssertJ VisibleForTesting](https://javadoc.io/doc/org.assertj/assertj-core/latest/org/assertj/core/util/VisibleForTesting.html)
39
-
-Assumptions of what level of access is permittable for each access modifier and the annotation: [JetBrains VisibleForTesting](https://javadoc.io/doc/org.jetbrains/annotations/22.0.0/org/jetbrains/annotations/VisibleForTesting.html)
0 commit comments