You need to sign in to do that
Don't have an account?
Swaroopa Akula 10
Test apex class - Code coverage
I have a trigger and i am trying to create a test class. But not sure how to write test class to fulfill the test coding coverage for test apex class
TRIGGER:
trigger opportunitytrigger on opportunity(after update){
Set<Id> oppIds = new Set<Id>();
// Set<Id> oppWithContractAgreement = new Set<Id>();
List<APXT_Redlining__Contract_Agreement__c> oppWithContractAgreement = new List<APXT_Redlining__Contract_Agreement__c>();
List<Opportunity> opportunity = new List<Opportunity>();
for(opportunity opp:Trigger.new)
{
if(opp.Stagename=='Closed Won')
{
oppIds.add(opp.id);
opportunity.add(opp);
}
}
List<APXT_Redlining__Contract_Agreement__c> con =[Select Id, Opportunity__c,APXT_Redlining__Status__c,Verified_Signed_IO__c from APXT_Redlining__Contract_Agreement__c where Opportunity__c IN :oppIds];
{
if(con.size()>0)
system.debug(+con.size());
{
for(APXT_Redlining__Contract_Agreement__c c :con)
{
if( c.APXT_Redlining__Status__c == 'Active' && c.Verified_Signed_IO__c ==FALSE){
oppWithContractAgreement.add(c);
system.debug('-----------'+oppWithContractAgreement.size());
}
}
}
}
For(Opportunity opps:opportunity)
{
if(oppWithContractAgreement.size()>0)
{
system.debug(oppWithContractAgreement.size());
opps.addError('Closed Won Opportunity must need to have active contract agreement and customer signed date captured or Closed Won Opportunity must need to have active contract agreement');
}
if(con.size()==0)
{
opps.addError('Closed Won Opportunity must need to have contract agreement' );
}
}
}
TEST CLASS:
@isTest
private class OpportunityTriggerHandler1_Test {
@isTest static void testCreateOpportunityTarget() {
//create Account
Account acc = new Account();
acc.Name = 'Test Account';
insert acc;
Test.startTest();
Opportunity opp = new Opportunity();
opp.AccountId = acc.Id;
opp.Name = 'Test Opportunity';
opp.StageName = 'Closed Won';
opp.CloseDate = Date.Today().addDays(20);
insert opp;
Test.stopTest();
List<APXT_Redlining__Contract_Agreement__c> CA = new List<APXT_Redlining__Contract_Agreement__c>();
CA = [Select Id, Opportunity__c,APXT_Redlining__Status__c,Verified_Signed_IO__c from APXT_Redlining__Contract_Agreement__c ];
System.assert(CA[0].Opportunity__c == opp.Id);
}
}
TRIGGER:
trigger opportunitytrigger on opportunity(after update){
Set<Id> oppIds = new Set<Id>();
// Set<Id> oppWithContractAgreement = new Set<Id>();
List<APXT_Redlining__Contract_Agreement__c> oppWithContractAgreement = new List<APXT_Redlining__Contract_Agreement__c>();
List<Opportunity> opportunity = new List<Opportunity>();
for(opportunity opp:Trigger.new)
{
if(opp.Stagename=='Closed Won')
{
oppIds.add(opp.id);
opportunity.add(opp);
}
}
List<APXT_Redlining__Contract_Agreement__c> con =[Select Id, Opportunity__c,APXT_Redlining__Status__c,Verified_Signed_IO__c from APXT_Redlining__Contract_Agreement__c where Opportunity__c IN :oppIds];
{
if(con.size()>0)
system.debug(+con.size());
{
for(APXT_Redlining__Contract_Agreement__c c :con)
{
if( c.APXT_Redlining__Status__c == 'Active' && c.Verified_Signed_IO__c ==FALSE){
oppWithContractAgreement.add(c);
system.debug('-----------'+oppWithContractAgreement.size());
}
}
}
}
For(Opportunity opps:opportunity)
{
if(oppWithContractAgreement.size()>0)
{
system.debug(oppWithContractAgreement.size());
opps.addError('Closed Won Opportunity must need to have active contract agreement and customer signed date captured or Closed Won Opportunity must need to have active contract agreement');
}
if(con.size()==0)
{
opps.addError('Closed Won Opportunity must need to have contract agreement' );
}
}
}
TEST CLASS:
@isTest
private class OpportunityTriggerHandler1_Test {
@isTest static void testCreateOpportunityTarget() {
//create Account
Account acc = new Account();
acc.Name = 'Test Account';
insert acc;
Test.startTest();
Opportunity opp = new Opportunity();
opp.AccountId = acc.Id;
opp.Name = 'Test Opportunity';
opp.StageName = 'Closed Won';
opp.CloseDate = Date.Today().addDays(20);
insert opp;
Test.stopTest();
List<APXT_Redlining__Contract_Agreement__c> CA = new List<APXT_Redlining__Contract_Agreement__c>();
CA = [Select Id, Opportunity__c,APXT_Redlining__Status__c,Verified_Signed_IO__c from APXT_Redlining__Contract_Agreement__c ];
System.assert(CA[0].Opportunity__c == opp.Id);
}
}
I review your code and found issues so fixed them in my code below with the test class 100% coverage. Please take a look on the code, compare it with yours and see the changes.
I see you are only running the trigger after update, are you also looking for after insert?
I have cover the test keeping the same context, I think the code should also work for after insert
test class
Regards,
Abdul Aziz Khatri
If you find my answer helpful, please mark it as the best answer. Thanks!
The test class you provided only tests the creation of an Opportunity and verifies that a related Contract Agreement record is created. However, it does not provide full coverage for the Opportunity trigger. To achieve full coverage, you need to write additional test methods that cover different scenarios and conditions within the trigger.
Please check with the below code and let me know further :
If this helps , please mark this as Best Answer.
Thank you.
Hi Swaroopa,
1. Trigger Class: (Code Coverage 100%)
2. Test Class:
Hope this will be helpful.
Thanks!