SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • ShellShell
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
Apex

Apex static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your APEX code

  • All rules 97
  • Vulnerability2
  • Bug26
  • Security Hotspot3
  • Code Smell66
Filtered: 1 rule found
encapsulation
    Impact
      Clean code attribute
        1. Private members made public for testing should use "@TestVisible" annotation

           Code Smell

        Private members made public for testing should use "@TestVisible" annotation

        adaptability - modular
        maintainability
        Code Smell
        • apex
        • salesforce
        • encapsulation
        • testing

        This rule raises an issue when private methods or variables are made public solely to enable access from test classes, compromising proper encapsulation.

        Why is this an issue?

        How can I fix it?

        More Info

        Making private members public just for testing breaks encapsulation, one of the fundamental principles of object-oriented programming. When you change a private method or variable to public, you expose internal implementation details that were intentionally hidden.

        Encapsulation helps maintain code quality by:

        • Hiding internal implementation details from other classes
        • Preventing unintended access to sensitive logic or data
        • Making it easier to refactor internal code without affecting external dependencies
        • Clearly defining the public interface of a class

        When private members are made public for testing, they become part of the class’s public API, even though they weren’t designed for external use. This can lead to:

        • Other developers accidentally using these "internal" methods in production code
        • Increased coupling between classes
        • Difficulty in refactoring, as changing these methods might break code that shouldn’t depend on them
        • Confusion about what constitutes the intended public interface

        Salesforce provides the @TestVisible annotation specifically to address this testing challenge. This annotation allows private members to be accessed from test classes while keeping them private in all other contexts. It’s a clean solution that maintains encapsulation while enabling comprehensive testing.

        What is the potential impact?

        Breaking encapsulation can lead to tightly coupled code that is harder to maintain and refactor. It may also result in unintended dependencies on internal implementation details, making the codebase more fragile and prone to bugs when internal logic changes.

          Available In:
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use