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
Ravindra Kashyap 2Ravindra Kashyap 2 

@istest(SeeAllData=true) and @testsetup not working

Hello everyone,

When I am trying to test my class using test setup method then IDE is giving an error. There is any way to avoid this and use both methods during test??

@IsTest
global class AddPrimaryContactTest 
{    
     @TestSetup
    static void setup()
    {
        List<Account> accountList = new List<Account>();
        for(Integer i = 1; i <= 50; i++)
        {
            accountList.add(new Account(name = 'Test Account'+ i, BillingState = 'CA'));
        }
        for(Integer i = 50; i <= 100; i++)
        {
            accountList.add(new Account(name = 'Test Account'+ i, BillingState = 'NY'));
        }
        insert accountList;
    }
    
    @IsTest(SeeAllData=true)
    global static  Void test()
    {
        Contact con = new Contact(Firstname ='conFirst',lastname='conLast');
        AddPrimaryContact obj = new AddPrimaryContact(con,'CG');        
        Test.startTest();
        System.enqueueJob(obj);
        Test.stopTest();
        List<Account> count = [select id,(select id from contacts) from Account where BillingState = 'CA'];
        System.debug('countcontact = ' + count.size() );
        System.assertEquals(50, count.size());        
    }
}


thanks in Advance :)
Best Answer chosen by Ravindra Kashyap 2
Ravindra Kashyap 2Ravindra Kashyap 2
I think I got the reason why it is showing error. We can't use @istest(SeeAllData=true) and @testsetup at same class because if we set "SeeAllData=true" then during the test it will check for the database(if program containing any soql or sosl) and if we create setup data using "@testsetup"  then it will rollback after testing. Means data will not get stored in the database. This is the reason compiler is not allowing to add both annotations in the same class.

Thanks Pradeep for link :)

All Answers

pradeep kumar yadavpradeep kumar yadav
You Cannot be used both annotations in the same test class.
Please refer.......https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm" target="_blank)
Ravindra Kashyap 2Ravindra Kashyap 2
I think I got the reason why it is showing error. We can't use @istest(SeeAllData=true) and @testsetup at same class because if we set "SeeAllData=true" then during the test it will check for the database(if program containing any soql or sosl) and if we create setup data using "@testsetup"  then it will rollback after testing. Means data will not get stored in the database. This is the reason compiler is not allowing to add both annotations in the same class.

Thanks Pradeep for link :)
This was selected as the best answer