5.3 TestVisible and TestSetup Annotation

Today, we will introduce two commonly used annotations for unit testing TestVisible and Test Annotation.

What is TestVisible Annotation

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.

What is TestSetup Annotation

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.

Here is a sample code:

@isTest
public class SampleTest 
{
    @isTest 
    static void Test1()
    {
        Account theAccount = [Select Id, Name From Account];
        System.assertEquals('Test', theAccount.Name);
    }

    @testSetup
    static void testPrep()
    {
        Account a = new Account(name='Test');
        insert a;
    }
}

The above code will work because the account generated at testPrep will be available to use for each test method.

But, it won’t store the value in static variables

The test data generated in database will be available for each test method to use. However, the static variable values won’t be kept. To make it clearer, we can take a look at the below code:


@isTest
public class SampleTestClass 
{
    public static String tmp;
    
    @isTest 
    static void Test1()
    {
        System.debug('tmp in Test1: ' + tmp);
    }

    @testSetup
    static void testPrep()
    {
        tmp = 'Hello';
        System.debug('tmp in prep: ' + tmp);
    }
}

The result will be as in the following screenshot:

We have talked about this in Salesforce StackExchange, the link is here.

Why?

Because, what testSetup method does is to write some data into Salesforce database and keep a snapshot of that. It will only be executed once at the beginning of the test class. So if the test method has modified the database, the changes will be rolled back at the end of each test method.

Please note that this only applies to the data which are persisted. It doesn’t store the static variable values. By doing that, it can make sure test methods don’t interfere with each other.

Pros and Cons about TestSetup Annotation

So, what are the Pros and Cons? Below is my understanding:

Pros:

  1. TestSetup method will only be executed once at the beginning of test class. So it will be marginally faster than the old school calling a static testPrep method at each test method way.
  2. The coding structure is clearer by following this.

Cons:

  1. Because static variables are not kept, it usually means we have to have extra SOQL queries at the beginning of each test method.
  2. The governor limits used in the testSetup method is added into each test method. (You can experiment around that)

 Next Post

5.4 Unit test data preparation framework

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 

Leave a Reply

Your email address will not be published / Required fields are marked *