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
balraj singh 33balraj singh 33 

how reuse the data inserted in test class

how can we save the data inserted through query in test class and use it again for testing
Raj VakatiRaj Vakati
you can do it like this ..setup data by using @testSetup and will be avilable across the places for entire test class
 
@testSetup
    static void setup()
    {
      Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100);
       System.debug('Price before inserting new book: ' + b.Price__c);

       // Insert book
       insert b;
        
        
    }
    @isTest
    private static void testCase1(){
       Book__c b =[Select Id from Book__c Limit 1 ] ;
// Execute your test logic here 	   
    }

 
Amit Chaudhary 8Amit Chaudhary 8
Hi balraj singh 33,

There are two way to reuse the Test Function/Test Data
1) Create Test Factory (http://amitsalesforce.blogspot.com/2017/01/test-utility-classes-testdatafactory.html)
2) Use @testSetup (http://amitsalesforce.blogspot.com/2015/05/testsetup-set-up-test-data-for-entire.html)

Example 1) Test Factory
@isTest
public with sharing class TestDataFactory 
{
    public static Lead createLead(Boolean doInsert)
    {
        Lead newLead = new Lead() ;
        newLead.FirstName = 'Cole';
        newLead.LastName = 'Swain';
        newLead.Company = 'BlueWave';
        newLead.Status = 'contacted';
        if(doInsert){
            insert newLead;
        }
        return newLead;
    }
    
    public static Account createAccount(Boolean doInsert)
    {
        Account acc = new Account();
        acc.Name = 'Test Account';
        if(doInsert){
            insert acc;
        }
        return acc;
    }
       
    public static Contact createContact(Boolean doInsert)
    {
        return createContact(doInsert, createAccount(true).Id);
    }
    
    public static Contact createContact(Boolean doInsert, Id accId)
    {
        Contact con = new Contact();
        con.AccountId = accId;
        con.FirstName = 'FirstName';
        con.LastName = 'LastName';
        con.Email = 'FirstName@test.com' + Math.floor(Math.random() * 1000);
        if(doInsert)
        {
            insert con;
        }
        return con;
    }

    
}

Test Class Should be like below
@isTest
private class MyTestClass 
{
    static testmethod void myUnitTest() 
    {
        Lead leadObj = TestDataFactory.createLead(true);
        Account accObj = TestDataFactory.createAccount(true);
        Contact contObj = TestDataFactory.createContact(true,accObj.id);
    }
}

Example 2) @testSetup ( Set Up Test Data for an Entire Test Class )
@isTest
private class CommonTestSetup 
{
 @testSetup 
 static void setup() 
 {
  Account acct = new Account();
       acct.Name = 'Salesforce.com';
       acct.Industry = 'Technology';
  insert acct;
  
  Contact cont = new Contact();
       cont.FirstName = 'Amit';
       cont.LastName = 'Chaudhary';
       cont.AccountId = acct.Id;
  insert cont;
 }
    
 @isTest 
 static void testMethod1() 
 {
  Account acct = [SELECT Id FROM Account WHERE Name='Salesforce.com' LIMIT 1];
     acct.Phone = '555-1212';
  update acct;
 }

 @isTest 
 static void testMethod2() 
 {
  Account acct = [SELECT Phone FROM Account WHERE Name='Salesforce.com' LIMIT 1];
  System.assertEquals(null, acct.Phone);
 }
}

Let us know if this will help you

 
Suraj Tripathi 47Suraj Tripathi 47
Hi balraj,

You can create a method by using @testSetup annotation in this method you can insert the record that you want to use again and again.
So that you can use these records by using queries in other methods of the same test class.

Syntax:

@testSetup
    static void setup()
    {
       //Records
    }

If you find your Solution then mark this as the best answer.

Thank you!

Regards,
Suraj Tripathi