-Guava has @VisibleForTesting /VisibleForTesting.html) annotation is provided --Grant when it is unavoidable to increase the visibility of methods and members in order to write test code ――It serves as a mark to show that "I am increasing visibility for the test code"
import com.google.common.annotations.VisibleForTesting;
public class Sample {
@VisibleForTesting
String load() {
//abridgement
}
}
A detailed usage example is explained in Let's break away from static methods to write unit tests # 1 Principle / Wrap method.
It can be used as a marker to show that "the visibility is increased because of the test code". Annotation alone does not have any special function, it is just a marker (called a marker annotation).
For example, one of the static analysis tools, SonarQube (https://www.sonarqube.org/), incorrectly added a member or method with @VisibleForTesting
(other than the test code). There is a rule to check if it is accessed by code of another class. By using this rule, it may be possible to detect unintended behavior at an early stage.
As stated in JavaDoc, the access range is originally set to default from ** private (package). Used for ** to change to (private). If you just write unit tests, the defaults are sufficient, and changing to public or protected is a bad design.
In addition, VisibleForTesting plugin for SonarQube can be used to check if an incorrect access modifier is added.
@VisibleForTesting
annotationsSome other libraries and APIs have similar annotations.
Library / API | Fully qualified class name |
---|---|
AssertJ | org.assertj.core.util.VisibleForTesting |
Android | androidx.annotation.VisibleForTesting |
Apache Flink | org.apache.flink.annotation.VisibleForTesting |
Recommended Posts