You need to sign in to do that
Don't have an account?
Sudhaaaa
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);
}
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);
}
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.
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
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.
Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.
Thanks and Regards
I hope you are doing well .....!!
Please use the below code:
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
In @testsetup only i am getting 100% code coverage. Then what is the use of insertNewCompanyInformation()?
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:
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