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
SudhaaaaSudhaaaa 

How to write @testSetup in ApexTestClass?

Hi, 
can anyone help me to write below TestClass with the help of @testSetup.
CompanyInformationTriggerHandlerTest:
@isTest
public class CompanyInformationTriggerHandlerTest {
    static testmethod void insertNewCompanyInformation(){
        Company_Information__c ci = new Company_Information__c();
        ci.Legal_Entity_Name__c ='Test123';
        ci.DBA_Name__c='Test DBA';
        ci.Business_Street_Address_Line_1__c='Address1';
        ci.Business_Street_Address_Line_2__c='Address2';
        ci.Business_Mailing_City__c='city';
        ci.Business_Mailing_Country__c='country';
        ci.Shipping_Street_Address_Line_1__c='Address3';
        ci.Business_Street_Address_Line_2__c='Address4';
        ci.Shipping_Mailing_City__c='Scity';
        ci.Shipping_Mailing_Country__c='Scountry';
        ci.Status__c='Active';
        insert ci;
    }

}
Handler Class:
public class CompanyInformationTriggerHandler {
    
    public void updatevalue(List<Company_Information__c> companyInfoList){
         List<Affirmation__c> af = new List <Affirmation__c>();
         for(Company_Information__c cf:companyInfoList)
         
        {
              Affirmation__c af1=new Affirmation__c(Name=cf.Name,
                                                    Legal_Entity_Name__c = cf.Legal_Entity_Name__c,
                                                    DBA_Name__c=cf.DBA_Name__c,
                                                    Business_Street_Address_Line_1__c=cf.Business_Street_Address_Line_1__c,
                                                      Business_Street_Address_Line_2__c=cf.Business_Street_Address_Line_2__c,
                                                    Business_Mailing_City__c=cf.Business_Mailing_City__c,
                                                    Business_Mailing_Country__c=cf.Business_Mailing_Country__c,
                                                    Shipping_Street_Address_Line_1__c=cf.Shipping_Street_Address_Line_1__c,
                                                      Shipping_Street_Address_Line_2__c=cf.Shipping_Street_Address_Line_2__c,
                                                    Shipping_Mailing_City__c=cf.Shipping_Mailing_City__c,
                                                    Shipping_Mailing_Country__c=cf.Shipping_Mailing_Country__c,
                                                       Company_Information__c=cf.Id
                                                    );
            if(cf.Status__c=='Active')
            {
                af1.Status__c='Draft';
            }
           af.add(af1);
    }
    insert af;
}
    }
Trigger:
trigger CompanyInformationTrigger on Company_Information__c (After insert, before update)
{
   CompanyInformationTriggerHandler handler = new CompanyInformationTriggerHandler();
        
    handler.updatevalue(Trigger.new);
}
Best Answer chosen by Sudhaaaa
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi reshma,
Generally testsetup method is used to create common test data  and use this method in test classes where you want same data.
Method marked with @TestSetUp annotation executes before any testMethod.
You just need to wrap the test data inside this testsetup method.
@istest
public class CompanyInformationTriggerHandlerTest {
    @testSetup static void setupmethod() {
			 Company_Information__c ci = new Company_Information__c();
        ci.Legal_Entity_Name__c ='Test123';
        ci.DBA_Name__c='Test DBA';
        ci.Business_Street_Address_Line_1__c='Address1';
        ci.Business_Street_Address_Line_2__c='Address2';
        ci.Business_Mailing_City__c='city';
        ci.Business_Mailing_Country__c='country';
        ci.Shipping_Street_Address_Line_1__c='Address3';
        ci.Business_Street_Address_Line_2__c='Address4';
        ci.Shipping_Mailing_City__c='Scity';
        ci.Shipping_Mailing_Country__c='Scountry';
        ci.Status__c='Active';
        insert ci;
    }
    @istest 
    public static void insertNewCompanyInformation(){
        list<Company_Information__c> acc = [select id,Legal_Entity_Name__c from Company_Information__c];
    }

}

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi reshma,
Generally testsetup method is used to create common test data  and use this method in test classes where you want same data.
Method marked with @TestSetUp annotation executes before any testMethod.
You just need to wrap the test data inside this testsetup method.
@istest
public class CompanyInformationTriggerHandlerTest {
    @testSetup static void setupmethod() {
			 Company_Information__c ci = new Company_Information__c();
        ci.Legal_Entity_Name__c ='Test123';
        ci.DBA_Name__c='Test DBA';
        ci.Business_Street_Address_Line_1__c='Address1';
        ci.Business_Street_Address_Line_2__c='Address2';
        ci.Business_Mailing_City__c='city';
        ci.Business_Mailing_Country__c='country';
        ci.Shipping_Street_Address_Line_1__c='Address3';
        ci.Business_Street_Address_Line_2__c='Address4';
        ci.Shipping_Mailing_City__c='Scity';
        ci.Shipping_Mailing_Country__c='Scountry';
        ci.Status__c='Active';
        insert ci;
    }
    @istest 
    public static void insertNewCompanyInformation(){
        list<Company_Information__c> acc = [select id,Legal_Entity_Name__c from Company_Information__c];
    }

}

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
Foram Rana RForam Rana R
Hi Reshma,

​​​​​​​I hope you are doing well .....!!
Please use the below code:
@isTest
public class CompanyInformationTriggerHandlerTest {
	@testSetup static void setup() {
	
		// Insert Company_Information__c Record
		Company_Information__c ci = new Company_Information__c();
		ci.Legal_Entity_Name__c ='Test123';
		ci.DBA_Name__c='Test DBA';
		ci.Business_Street_Address_Line_1__c='Address1';
		ci.Business_Street_Address_Line_2__c='Address2';
		ci.Business_Mailing_City__c='city';
		ci.Business_Mailing_Country__c='country';
		ci.Shipping_Street_Address_Line_1__c='Address3';
		ci.Business_Street_Address_Line_2__c='Address4';
		ci.Shipping_Mailing_City__c='Scity';
		ci.Shipping_Mailing_Country__c='Scountry';
		ci.Status__c='Active';
		insert ci;
		
	}
	
	static testmethod void insertNewCompanyInformation(){
        
		//fetch record that we have insert in setup() 
		Company_Information__c obj = [SELECT Legal_Entity_Name__c, DBA_Name__c, Business_Street_Address_Line_1__c, Business_Street_Address_Line_2__c, Business_Mailing_City__c, Business_Mailing_Country__c,  Shipping_Street_Address_Line_1__c, Shipping_Mailing_City__c, Shipping_Mailing_Country__c, Status__c FROM Company_Information__c WHERE Legal_Entity_Name__c='Test123' LIMIT 1];
		
    }
}



Hope this helps you.
Let me know your review.
If this helps kindly mark it as solved so that it may help others in the future.

Thanks & Regards,
Foram Rana
SudhaaaaSudhaaaa
Hi Devi Chandrika/  Rana
In @testsetup only i am getting 100% code coverage. Then what is the use of  insertNewCompanyInformation()?
Foram Rana RForam Rana R
Means you insert record once in @testsetup and use it multiple times a query on it throughout your apex class.
 
Andrew GAndrew G
If you consider the nature of your trigger, it is 
On Insert of Record, Run the code.  Hence why you have 100% coverage.

If the trigger or class was more complex, then the true benefit of the @TestSetup becomes more obvious.  For example, a trigger on a contact doing multiple actions.  In the Test Setup, create the Account record so that it exists.  And locate it for each test method that follows.

In the example you have, if we leave the the test setup as is, as the others have done it nicely, consider the following:
 
@IsTest
public static void checkNewDraftAffirmation(){
    List<Affirmation__c > affList = [SELECT Id,Legal_Entity_Name__c, Status FROM Affirmation__c];
    System.assertEquals(1,affList.size());  //sanity check
    System.assertEquals('Draft',affList[0].Status__c);
}
@IsTest
public static void checkAffirmationNoStatus(){
    List<Company_Information__c> ciList= [SELECT id,Legal_Entity_Name__c,Status FROM Company_Information__c];
    System.assertEquals(1,ciList.size());  //sanity check
    ciList[0].Status__c = 'Inactive';
    update acc;
    
    List<Affirmation__c > affList = [SELECT Id,Legal_Entity_Name__c, Status FROM Affirmation__c];
    System.assertEquals(1,affList.size());//sanity check
    System.assertEquals('',affList[0].Status__c);
}

So now I have a tests to cover both options of the IF statement in the code you are using.   But, I only created the Company Information record once.  
The other benefit is that during the test run, the creation of each test record has over head.  But inserting the test records only once, you improve the run time of the tests.  Making deployment quicker.  etc.

Note, good test code has asserts, not just coverage.

Regards

Andrew