You need to sign in to do that
Don't have an account?
Vishnu 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 :)
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.
Please mark it as the Best Answer if it helps you.
Thank you.
All Answers
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!
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.
Please mark it as the Best Answer if it helps you.
Thank you.
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:-
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
Try Below Code Please Mark It As Best Answer If It Helps You.
Thank You!