Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-5803

Class members annotated with "@VisibleForTesting" should not be accessed from production code

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this usage of {identifier name}, it is annotated with @VisibleForTesting and should not be accessed from production code.
    • Highlighting:
      Hide

      Visible for testing identifier name (methods or fields)

      Show
      Visible for testing identifier name (methods or fields)
    • Default Severity:
      Critical
    • Impact:
      High
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • Analysis Scope:
      Main Sources

      Description

      @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
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              quentin.jaquier Quentin Jaquier
              Votes:
              2 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated: