You need to sign in to do that
Don't have an account?
Janaki 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
My Test class
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; } }
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.
I have added a code for you.
May be it will help you.
All Answers
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
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.
My Trigger Handler
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.
I have added a code for you.
May be it will help you.
Thank you Rushita, that worked pretty well,