+ Start a Discussion
Christian Carvajal 9Christian Carvajal 9 

@isTest(SeeAllData=true)

I have test classes that have been built using the @IsTest(SeeAllData=true) however, to my understanding it's not the best practice. Do I need to rewrite new test.classes using @IsTest(SeeAllData=false) or can I just replace the original code and then redeploying via a force.com tool?
Best Answer chosen by Christian Carvajal 9
Sanjay Bhati 95Sanjay Bhati 95
Hi Christian,

Please remove the SeeAllData = true from the @isTest create one setup method using @Testsetup annotation and insert all the needed data in that method. Now create your method with @isTest annotation and query the data you will get the data inserted in setup method.

Like below code
@TestSetup
public static void setupMethod()
{
    Account acc = new Account();
    acc.Name = 'test';
    insert acc;
}

@isTest
public static testMethod void method1()()
{
    List<Account> accList = [Select Id,Name From Account];
    // here you will get 1 record of account that we inserted in setup method
}
If it works for you then select as best answer
Thanks
 

All Answers

Sanjay Bhati 95Sanjay Bhati 95
Hi Christian,

Please remove the SeeAllData = true from the @isTest create one setup method using @Testsetup annotation and insert all the needed data in that method. Now create your method with @isTest annotation and query the data you will get the data inserted in setup method.

Like below code
@TestSetup
public static void setupMethod()
{
    Account acc = new Account();
    acc.Name = 'test';
    insert acc;
}

@isTest
public static testMethod void method1()()
{
    List<Account> accList = [Select Id,Name From Account];
    // here you will get 1 record of account that we inserted in setup method
}
If it works for you then select as best answer
Thanks
 
This was selected as the best answer
Christian Carvajal 9Christian Carvajal 9
That was very helpful. Couldn't all this be done without creating a new apex class? Couldn't I just reuse the same class just replace what needs to be removed?