+ Start a Discussion
m 3m 3 

what is useful of @istest,@testVisible, @testSetUp as well.

Raj VakatiRaj Vakati
@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.

Example 

 
public class TestVisibleExample {
    // Private member variable
    @TestVisible private static Integer recordNumber = 1;

    // Private method
    @TestVisible private static void updateRecord(String name) {
        // Do something
    }
}


This is the test class that uses the previous class. It contains the test method that accesses the annotated member variable and method.
 
@isTest
private class TestVisibleExampleTest {
    @isTest static void test1() {
        // Access private variable annotated with TestVisible
        Integer i = TestVisibleExample.recordNumber;
        System.assertEquals(1, i);

        // Access private method annotated with TestVisible
        TestVisibleExample.updateRecord('RecordName');
        // Perform some verification
    }
}


TestSetup Annotation



Methods defined with the @testSetup annotation are used for creating common test records that are available for all test methods in the class.
 
@testSetup static void methodName() {

}



https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testsetup.htm


IsTest Annotation


Use the @isTest annotation to define classes and methods that only contain code used for testing your application. The@isTest annotation on methods is equivalent to the testMethod keyword. The @isTest annotation can take multiple modifiers within parentheses and separated by blanks.​​​​​​​
 
@isTest
private class MyTestClass {

   // Methods for testing
   @isTest static void test1() {
      // Implement test code
   }

   @isTest static void test2() {
      // Implement test code
   }

}

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_isTest.htm​​​​​​​