You need to sign in to do that
Don't have an account?
Babu sfdc
How to write a test class for helper class
Please find my trigger and test class in code sample and suggest me how to improve code coverage for helper class.
Apex Trigger : trigger OpportunityLineItemCheck on OpportunityLineItem (before insert) { opportunityLineItemCheckOperations.checkOnOpportunityLineItems(trigger.new); } Helper class : public class OpportunityLineItemCheckOperations { public static void checkOnOpportunityLineItems(List<OpportunityLineItem> opportunityProductList){ List<OppWrapper> lstOpps = new List<OppWrapper>(); Set<String> oppIds = new Set<String>(); Set<String> productcodeids = new Set<String>(); for (OpportunityLineItem oppline : opportunityProductList){ productcodeids.add(oppline.productcode); oppIds.add(oppline.OpportunityId); } List<Opportunity> lstOppos = [SELECT Id, (SELECT Id, opportunityid, productcode , Product2Id FROM OpportunityLineItems WHERE productcode in :productcodeids) FROM Opportunity WHERE Id in :oppIds ]; for (Opportunity oppor : lstOppos){ for (OpportunityLineItem oppline : oppor.OpportunityLineItems){ OppWrapper oppo = new OppWrapper(); oppo.OppID = oppor.Id; oppo.productcode = oppline.productcode; //oppo.ProductID = oppline.Product2Id; lstOpps.add(oppo); } } lstOpps.sort(); for (OpportunityLineItem oppline: opportunityProductList){ for(OppWrapper oppwrap :lstOpps){ if (oppline.OpportunityId == oppwrap.OppID && oppline.productcode == oppwrap.productcode) oppline.addError('This product is already added to this opportunity.You cannot add the same product again.'); /*else if (oppline.OpportunityId == oppwrap.OppID && oppline.Product2Id > oppwrap.ProductID) break; else if (oppline.OpportunityId > oppwrap.OppID) break;*/ } } } public class OppWrapper implements Comparable{ String OppID; String ProductID; String productcode; public Integer compareTo(Object compareTo){ OppWrapper compareToCopy = (OppWrapper) compareTo; Integer returnValue = 0; if (this.OppID > compareToCopy.OppID) returnValue = 1; else if (this.OppID < compareToCopy.OppID) returnValue = -1; else if (this.OppID == compareToCopy.OppID){ if (this.ProductID > compareToCopy.ProductID) returnValue = 1; else if (this.ProductID < compareToCopy.ProductID) returnValue = -1; } return returnValue; } } }
Please find the test class snippet in above and suggest