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
Janaki RaoJanaki Rao 

Not Able to do code coverage for the below Apex class

Hello Folks

i am new to the apex development, i m trying to write test class for the below apex class but not able to do the code coverage, Any help will be appreciated

Apex class
 
public class AggregateApprovalHelper {
    public static void updateOpportunities(Map<Id,Aggregate_Approval__c> newMap, Map<Id,Aggregate_Approval__c> oldMap){
        List<Id> approvedAggregateIds = new List<Id>();
        Map<Id, Aggregate_Approval__c> apprMap = new Map<Id, Aggregate_Approval__c>(); 
        for(Id appId : newMap.keySet()){
            if(newMap.get(appId).Status__c != oldMap.get(appId).Status__c && (newMap.get(appId).Status__c == 'Approved') || newMap.get(appId).Status__c == 'Rejected'){
                approvedAggregateIds.add(appId);
                apprMap.put(appId,newMap.get(appId));
            }
        }
        if(apprMap.size()>0){
       //     BatchUpdateOpportunities bat = new BatchUpdateOpportunities(apprMap);
       //     Database.executeBatch(bat);
        }
        if(approvedAggregateIds.size()>0){
            List<Opportunity> opps = [Select Id, StageName, Aggregate_Approval__r.Status__c from Opportunity where Aggregate_Approval__c IN :approvedAggregateIds];
            List<Opportunity> oppsToBeApproved = new List<Opportunity>();
            for(Opportunity o : opps){
               
                o.Status__c = o.Aggregate_Approval__r.Status__c;
                if(o.Aggregate_Approval__r.Status__c == 'Approved')
                	o.Billing_To_Be_Initiated__c ='Yes';
                else if(o.Aggregate_Approval__r.Status__c == 'Rejected')
                    o.Billing_To_Be_Initiated__c ='No';
                oppsToBeApproved.add(o);
            }
            if(oppsToBeApproved.size()>0){
                 List<Approval.UnlockResult> ur = Approval.unlock(oppsToBeApproved);
                update oppsToBeApproved;
            }
        }
    }
}

My Test class 
 
@isTest
public class AggregateApprovalTestclass {
    
    static testmethod void Method(){

    List<Aggregate_Approval__c> AggreApp = new list <Aggregate_Approval__c>();
    Aggregate_Approval__c Agg = new Aggregate_Approval__c(); 
   
    Agg.Month__c = 'October';
    Agg.Next_Month_Start_Date__c = system.today();
    Agg.Status__c = 'Created';
    Agg.Year__c = '2020';   
    AggreApp.add(Agg);
    insert Agg;
        
    }
    
    static testmethod void Method1(){ 
        List<Opportunity> Oppps = new list <Opportunity>();  
        Opportunity oop = new Opportunity(); 
        Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;
        User u = [Select id,name from user where isactive=true Limit 1];
        oop.Name = 'Test1';
        oop.AccountId = testAcct.ID;
        oop.StageName = 'Demo';
        oop.CloseDate = system.today();
        oop.LeadSource = 'Customer';
        oop.Country__c = 'India';
        Oppps.add(oop);
        insert oop;
    }
}

 
Best Answer chosen by Janaki Rao
Rushita Bavishi 1Rushita Bavishi 1
Hi Janki,
First your trigger is on update So, it will never call on insert. And You should not call Helper class to increase code coverage.
When you will update record then trigger will automatically called and your helper class will be called from trigger.
 
static testmethod void ApprovalTest1(){

        List<Aggregate_Approval__c> AggreApp = new list <Aggregate_Approval__c>();
        Aggregate_Approval__c Agg = new Aggregate_Approval__c(); 
		
	Agg.Month__c = 'October';
        Agg.Next_Month_Start_Date__c = system.today();
        Agg.Status__c = 'Approved';
        Agg.Year__c = '2020';   
        AggreApp.add(Agg);
        insert Agg;
       
	Agg.Status__c = 'Rejected';
	update Agg;
		
		
        Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;

        User u = [Select id,name from user where isactive=true Limit 1];
		
        List<Opportunity> Oppps = new list <Opportunity>(); 
	Opportunity oop = new Opportunity();

        oop.Name = 'Test1';
        oop.AccountId = testAcct.ID;
	oop.StageName = 'Demo';
        oop.CloseDate = system.today();
        oop.Billing_To_Be_Initiated__c = 'Yes';
        oop.LeadSource = 'Customer';
	oop.Aggregate_Approval__c  = Agg.Id;
        oop.Country__c = 'India';

        Oppps.add(oop);
        insert oop;
    }


I have added a code for you.
May be it will help you.

All Answers

AnudeepAnudeep (Salesforce Developers) 
HI Janaki, 

You have to ensure the opportunity you create in your test class meets the criteria (Status - Approved/Rejected) defined in your AggregateApprovalHelper to populate the below methods

approvedAggregateIds.add(appId); apprMap.put(appId,newMap.get(appId));

Criteria
======

if(newMap.get(appId).Status__c != oldMap.get(appId).Status__c && (newMap.get(appId).Status__c == 'Approved') || newMap.get(appId).Status__c == 'Rejected'){

Anudeep
Janaki RaoJanaki Rao
@Anudeep

Can you please elaborate and specify what am i doing wrong?

i have modified my Test Class, i am getting 90% coverage for the helper class, but i am not able to cover for the  Trigger. 
 
@isTest
public class AggregateApprovalTestclass {
    
    static testmethod void Method(){
        
        List<Aggregate_Approval__c> AggreApp = new list <Aggregate_Approval__c>();
        Aggregate_Approval__c Agg = new Aggregate_Approval__c(); 
        
        Agg.Month__c = 'October';
        Agg.Next_Month_Start_Date__c = system.today();
        Agg.Status__c = 'Approved';
        Agg.Year__c = '2020';   
        AggreApp.add(Agg);
        insert Agg;
        Aggregate_Approval__c Agg2 = new Aggregate_Approval__c();
        Agg2.Id = Agg.Id;
        Agg2.Month__c = 'October';
        Agg2.Next_Month_Start_Date__c = system.today();
        Agg2.Status__c = 'Demo';
        Agg2.Year__c = '2020'; 
        List<Opportunity> Oppps = new list <Opportunity>();  
        Opportunity oop = new Opportunity(); 
        Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;
        User u = [Select id,name from user where isactive=true Limit 1];
        oop.Name = 'Test1';
        oop.AccountId = testAcct.ID;
        oop.StageName = 'Demo';
        oop.CloseDate = system.today();
        oop.Billing_To_Be_Initiated__c = 'Yes';
        oop.LeadSource = 'Customer';
        oop.Country__c = 'India';
        oop.Aggregate_Approval__c = Agg.id ;
        Oppps.add(oop);
        insert oop;
        Map<Id,Aggregate_Approval__c> newMap = New Map<Id,Aggregate_Approval__c>(); 
        newMap.put(Agg.ID, Agg);
        Map<Id,Aggregate_Approval__c> oldMap = New Map<Id,Aggregate_Approval__c>(); 
        oldMap.put(Agg.ID, Agg2);
        AggregateApprovalHelper.updateOpportunities(newMap, oldMap);
        AggregateApprovalHelper.updateOpportunities(newMap, oldMap);
        
    }
    
    static testmethod void Method1(){ 
        List<Opportunity> Oppps = new list <Opportunity>();  
        Opportunity oop = new Opportunity(); 
        Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;
        User u = [Select id,name from user where isactive=true Limit 1];
        oop.Name = 'Test1';
        oop.AccountId = testAcct.ID;
        oop.StageName = 'Demo';
        oop.CloseDate = system.today();
        oop.Billing_To_Be_Initiated__c = 'Yes';
        oop.LeadSource = 'Customer';
        oop.Country__c = 'India';
        Oppps.add(oop);
        insert oop;
    }
}

My Trigger Handler
 
trigger AggregateApprovalTrigger on Aggregate_Approval__c (after update) {
    //if(Trigger.isafter && Trigger.isupdate){
    AggregateApprovalHelper.updateOpportunities(Trigger.newMap, Trigger.oldMap);
 //} uncomment if more events are added
}

 
Rushita Bavishi 1Rushita Bavishi 1
Hi Janki,
First your trigger is on update So, it will never call on insert. And You should not call Helper class to increase code coverage.
When you will update record then trigger will automatically called and your helper class will be called from trigger.
 
static testmethod void ApprovalTest1(){

        List<Aggregate_Approval__c> AggreApp = new list <Aggregate_Approval__c>();
        Aggregate_Approval__c Agg = new Aggregate_Approval__c(); 
		
	Agg.Month__c = 'October';
        Agg.Next_Month_Start_Date__c = system.today();
        Agg.Status__c = 'Approved';
        Agg.Year__c = '2020';   
        AggreApp.add(Agg);
        insert Agg;
       
	Agg.Status__c = 'Rejected';
	update Agg;
		
		
        Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;

        User u = [Select id,name from user where isactive=true Limit 1];
		
        List<Opportunity> Oppps = new list <Opportunity>(); 
	Opportunity oop = new Opportunity();

        oop.Name = 'Test1';
        oop.AccountId = testAcct.ID;
	oop.StageName = 'Demo';
        oop.CloseDate = system.today();
        oop.Billing_To_Be_Initiated__c = 'Yes';
        oop.LeadSource = 'Customer';
	oop.Aggregate_Approval__c  = Agg.Id;
        oop.Country__c = 'India';

        Oppps.add(oop);
        insert oop;
    }


I have added a code for you.
May be it will help you.
This was selected as the best answer
Janaki RaoJanaki Rao
@Wow
Thank you Rushita, that worked pretty well,