+ Start a Discussion
sobhan sfdcsobhan 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;     
        
        }
    
    }

}

 
Aman MalikAman Malik
Seems you forgot to add test class in snippet.
sobhan sfdcsobhan sfdc
@isTest(seeAllData=true)
private class Test_OpportunityLineItemCheck 
{
  static testMethod void Checkduplicate() 
  {
  
  
           Test.startTest();
        User currentUser = [Select Id, UserCountry__c from User where Id = :UserInfo.getUserId() limit 1];
        
        Account accP = new Account(Name = 'Partner1', Account_Type__c = 'VAR - MANAGED', RecordTypeId='01280000000Ln6i', Business_Unit__c = 'CBU', BillingCountry = country);

        insert acc;

        PricebookEntry[] pbes = [Select Id, UnitPrice, CurrencyIsoCode, Pricebook2Id from PricebookEntry where 
            IsActive = true];
            
            Opportunity opp4 = new Opportunity(Name= 'Opp31',Pricebook2Id = pbes[0].Pricebook2Id,RecordTypeId = '01280000000Lnks');
             
        Opportunity[] opplist=new Opportunity[]{opp4};
        insert opplist;
                        
        OpportunityLineItem oli4 = new OpportunityLineItem(OpportunityId = opp4.Id, PricebookEntryId = pbes.get(0).Id);
        
        OpportunityLineItem[] olilist=new OpportunityLineItem[]{oli4};
		
        insert olilist;
        
        opp4.SBA_Approval__c='Submitted';
        
        update opp4;
        Test.stopTest();
  
  }
  
  
 }

 
sobhan sfdcsobhan sfdc
Hi Aman Malik,


Please find the test class snippet in above and suggest