Activity Forums Salesforce® Discussions @testVisible and @testSetup in salesforce

  • shradha jain

    Member
    August 30, 2018 at 5:02 am

    Hello Madhulika,

    @testVisible: Some methods are declared as private so you won’t be able to call them outside the class. However, sometimes you really want to be able to invoke it from a unit test method.To resolve this, you can mark the method with a @TestVisible annotation. Then although it is still private, it is invokable from your unit test methods.

    @testSetup: For most unit tests in Salesforce, we need to generate some test data. And for different test methods within one single test class, the required test data set is usually very close. In order to serve for this purpose, Salesforce has introduced TestSetup annotation. The data generated in TestSetup will be persisted for every test method within the test class to use. And the data will only be rolled back after the execution of the whole test class completes.Methods defined with the @testSetup annotation are used for creating common test records that are available for all test methods in the class.
    Thanks.

  • shariq

    Member
    September 17, 2018 at 9:52 pm

    Hi,

    TestVisible  - Use the TestVisible annotation to allow test methods to access private or protected members of another class outside the test class. These members include methods, member variables, and inner classes. This annotation enables a more permissive access level for running tests only. This annotation doesn’t change the visibility of members if accessed by non-test classes.

    With this annotation, you don’t have to change the access modifiers of your methods and member variables to public if you want to access them in a test method. For example, if a private member variable isn’t supposed to be exposed to external classes but it  should be accessible by a test method, you can add the TestVisible annotation to the variable definition.

    testSetup  - Methods defined with the @testSetup annotation are used for creating common test records that are available for all test methods in the class.
    Syntax
    Test setup methods are defined in a test class, take no arguments, and return no value. The following is the syntax of a test setup method.

    @testSetup static void methodName() {

    }

    Hope this helps.

  • Parul

    Member
    September 18, 2018 at 8:23 pm

    @TestVisible Annotation.

    This annotation enables a more permissive access level for running tests only. TestVisible annotation allow test methods to access private or protected members of another class outside the test class. These members include methods, member variables, and inner classes.

    @testSetup: For most unit tests in Salesforce, we need to generate some test data. And for different test methods within one single test class, the required test data set is usually very close. In order to serve for this purpose, Salesforce has introduced TestSetup annotation. The data generated in TestSetup will be persisted for every test method within the test class to use. And the data will only be rolled back after the execution of the whole test class completes.Methods defined with the @testSetup annotation are used for creating common test records that are available for all test methods in the class.

     

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos