This plugin for SonarQube allows developers to detect common mistakes with Guava's @VisibleForTesting
annotation.
It detects illegal access of annotated members and methods as well as illegal use of guava's @VisibleForTesting
annotation.
Checks if production code accesses members or methods, that are annotated with @VisibleForTesting
.
The annotation's goal is to mark members or methods, whose visibility is increased from private
to default only for testing purposes. If production code acesses those members or methods, this may lead to unexpected behaviours.
Compliant code
/** src/main/java/MyObject.java */
@VisibleForTesting
String foo;
/** src/test/java/MyObjectTest.java */
new MyObject().foo; // --> ok, accesses member from test code
Noncompliant code
/** src/main/java/MyObject.java */
@VisibleForTesting
String foo;
/** src/main/java/Service.java */
new MyObject().foo; // --> not ok, accesses member from production code
Checks if @VisibleForTesting
is only used on members or methods with default visibility, as its purpose is to mark an increased visibility from private
to default.
Compliant code
@VisibleForTesting
String foo;
Noncompliant code
@VisibleForTesting
private String foo;
@VisibleForTesting
protected String foo;
@VisibleForTesting
public String foo;
- SonarQube 6+ (maybe below but not tested)
- Java 8
- Download the latest release's plugin jar and put it into the
extensions\plugins
folder of your SonarQube instance. - Restart SonarQube.
- Add the rules to your quality profile.
- SonarQube 6 compatibility
- fixed some administrative properties in the POM file
- Initial release