What is Guava's @VisibleForTesting?

Overview

-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"

Sample 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.

Benefits of granting @VisibleForTesting

It serves as a marker to show why you are increasing visibility

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).

You can check for errors with static analysis

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.

Rule S5803: Class members annotated with @VisibleForTesting should not be accessed from production code

important point

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.

Other @VisibleForTesting annotations

Some 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

What is Guava's @VisibleForTesting?
What is Cubby
What is Docker?
What is java
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is Maven Assembly?
What is `docker-compose up`?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is permission denied?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is object orientation?
What is MVC model?
What is an annotation?
What is Java technology?
What is Java API-java
What is @ (instance variable)?
What is Gradle's Artifact?
What is JPA Auditing?
[Swift] What is dismiss?
[Java] What is flatMap?
What is a Servlet?
What is web development?
[Java] What is JavaBeans?
[Android] What is Context? ??
[Java] What is ArrayList?
[Ruby] What is true?
What is object-oriented after all?
What is HttpSession session = request.getSession ();
What is docker run -it?
What is Java Assertion? Summary.
[Memorandum] What is an error?
What is a wrapper class?