function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vishnu SanthoshVishnu Santhosh 

'Variable does not exist' Error while using @testSetup in Apex Classes

Hi,

I am new to Salesforce Apex Development and also apex test classes. While i was going through test classes, I got an error. I will explain the logic and scenario : 

@isTest
public class testing(){
  
  @testSetup static void setup(){

   List<id> testId = new List<id>();
   List<Account> testData = new List<Account>();
   List<Account> retrievedData = new List<Account>();
   /**
       some logic is done and testData now has an Account in it
    */
   insert testData;
   }

  @isTest static void testAcc(){
        testData.some function();
      
        retrievedData.somefunction();

        testId.somefunction();
       
      Test.startTest();
      // Logic to query the account inserted
      Test.stopTest();

     }
}

 When i runs the code based on above logic, I get the error that Variable does not exist : testData.Same applies for testId, retrievedData.

How to fix the error? Am i doing the testSetup wrong ? Please guide me on this.

Thank you :)
 

Best Answer chosen by Vishnu Santhosh
Suraj Tripathi 47Suraj Tripathi 47

Hi Vishnu,

How can you use one variable from one method to another method Like testData is declared in setup method after that you can not access in other method.

as you are using @testSetup so you can query testData  records.

List<Account> testData2=[Select id from Account];
 

Please mark it as the Best Answer if it helps you.

Thank you.

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Vishnu,

We create testSetup Method so that data created in this method doesn’t need to be created again and again, and it is by default available for all test methods. Its not like any variable or list declared in testSetup Method can be accessed outside of it.

Details about testSetup method

https://www.sfdcpoint.com/salesforce/testsetup-method-in-apex-test-classes/

Hope it helps, Please marks its as best answer so that other facing similar issue find it useful.

Thanks!
Suraj Tripathi 47Suraj Tripathi 47

Hi Vishnu,

How can you use one variable from one method to another method Like testData is declared in setup method after that you can not access in other method.

as you are using @testSetup so you can query testData  records.

List<Account> testData2=[Select id from Account];
 

Please mark it as the Best Answer if it helps you.

Thank you.

This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
Hi,

In the @testsetup method create all data that is required to satisfies the condition in the actual class. 

So in testAcc() method you must fetch the data from the data that you have for example:-
@testSetup static void setup() {
        // Create common test accounts
        List<Account> testAccts = new List<Account>();
        for(Integer i=0;i<2;i++) {
             account ac = new account();
            ac.Name = 'TestAcct'+i;
            testAccts.add(ac);
        }
        insert testAccts;
}

@istest static void testMethod(){

List<account> accList = [select id, name from account];
if(accList.size()>0){
  test.startest();
   class_name.method_Name(accList);   // if list is required as a parameter in original class
   test.stoptest();
}
}

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

 
CharuDuttCharuDutt
Hii Vishnu Santosh
Try Below Code
@isTest
public class testing(){
@testSetup static void setup() {
       
        List<Account> lstAcc = new List<Account>();
        for(Integer i=0;i<10;i++) {
             Account Acc = new Account();
            Acc.Name = 'Test Account'+i;
/*fill Other Required Fields*/
            lstAcc .add(Acc);
        }
        insert lstAcc ;
}

@istest static void testMethod(){

List<account> getacc = [select id,name from Account];
if(getacc.size()>0){
  
   ClassName.MethodName(getacc);   /*If Parameter In class
else  ClassName.MethodName();*/
   System.debug(getacc);
}
}
}
Please Mark It As Best Answer If It Helps You.
Thank You!