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
Babu sfdcBabu 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) 

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){
        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;
        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)
                else if (oppline.OpportunityId > oppwrap.OppID)
    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.
Babu sfdcBabu sfdc
private class Test_OpportunityLineItemCheck 
  static testMethod void Checkduplicate() 
        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;
        update opp4;

Babu sfdcBabu sfdc
Hi Aman Malik,

Please find the test class snippet in above and suggest