@VisibleForTesting can be used to mark methods, fields and classes whose visibility restrictions have been relaxed more than necessary for the API to allow for easier unit testing.

Access to such methods, fields and classes only possible thanks to this relaxed visibility is fine for test code, but it should be avoided in production code. In production code these methods should be treated as if they are private.

Supported framework:

* Guava: com.google.common.annotations.VisibleForTesting

* AssertJ: org.assertj.core.util.VisibleForTesting

* Android: androidx.annotation.VisibleForTesting

* Apache Flink: org.apache.flink.annotation.VisibleForTesting

or any other annotation named VisibleForTesting

Noncompliant Code Example

/** src/main/java/MyObject.java */

@VisibleForTesting String foo;

/** src/main/java/Service.java */

new MyObject().foo; // Noncompliant, foo is accessed from production code

Compliant Solution

/** src/main/java/MyObject.java */

@VisibleForTesting String foo;

/** src/test/java/MyObjectTest.java */

new MyObject().foo; // Compliant, foo is accessed from test code