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
Kumar GKumar G 

How to improve code coverage for AddError method and Wrapper class

@isTest(seeAllData=true)
private class Test_OpportunityLineItemCheck 
{
  static testMethod void Checkduplicate() 
  { 
       Test.startTest();                       
        Account accP = new Account(Name = 'Partner1', Account_Type__c = 'VAR - MANAGED', RecordTypeId='01280000000Ln6i', Business_Unit__c = 'CBU');

        insert accP;
        
        PricebookEntry[] pbes = [Select Id, UnitPrice, CurrencyIsoCode, Pricebook2Id from PricebookEntry where IsActive = true AND UnitPrice > 0 
            AND CurrencyIsoCode = 'USD' AND Name like 'UK%'];
            
            Opportunity opp4 = new Opportunity(Name= 'Opp31',Pricebook2Id = pbes[0].Pricebook2Id,RecordTypeId = '01280000000Lnks');

        insert opp4;
                        
        OpportunityLineItem oli4 = new OpportunityLineItem(OpportunityId = opp4.Id, PricebookEntryId = pbes.get(0).Id, Quantity = 1, SBA_Price__c=20, GovEd__c=0, Partner_Discount__c=0, Promo_Discount__c=0, Deal_Reg_Discount__c=0);
        
        OpportunityLineItem[] olilist=new OpportunityLineItem[]{oli4};
      
        insert olilist;
        
        OpportunityLineItemCheckOperations.OppWrapper empW = new OpportunityLineItemCheckOperations.OppWrapper();
         empW.compareTo(empW);  
       Test.stopTest();
  }    
 }

I have created the Test class mentioned in the code sample it's having only 69% of code coverage , plz suggest me how to increase code coverage. 
Kumar GKumar G
Please find the class in code sample.
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;
                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.');
            }
            
        }
        
        
    }
    
    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;     
        
        }
    
    }

}
Thanks..
Aman MalikAman Malik
Hi,
Could you please highlight the section which is not being covered by your test class.

Thanks,
Kumar GKumar G
Please find uncovered code in screen shortUncovered code
Aman MalikAman Malik
Is OpportunityLineItemCheckOperations is a helper class for trigger?
If yes, is your trigger on Opportunity?
Kumar GKumar G
Hi Aman,

Yes it's an helper class , trigger is on OpportunityLineItem.

Apex Trigger :

 trigger OpportunityLineItemCheck on OpportunityLineItem (before insert) 
  {
    opportunityLineItemCheckOperations.checkOnOpportunityLineItems(trigger.new);
  }

Thanks.,
v varaprasadv varaprasad
Hi Kumar,

Please assign values to wrapper class fields :
OpportunityLineItemCheckOperations.OppWrapper empW = newOpportunityLineItemCheckOperations.OppWrapper();
empW .OppID = opp4.id;
empW .OppID = '12347';
empW.compareTo(empW); 


Please check once above code and let me know code work or not.


Thanks
Varaprasad

 
Kumar GKumar G
Hi Vara,

I have checked with the above code i am getting the below error : 

Compile Error: Variable is not visible: OpportunityLineItemCheckOperations.OppWrapper.OppID at line 29 column 15


Thanks
Naveen
v varaprasadv varaprasad
Hi Kumar,

Add public to your strings in wrapper class  :

public   String OppID;
 public    String ProductID;
public     String productcode;

Thanks
Varaprasad
Kumar GKumar G
Hi Vara,

I have added public to strings and assigned values to wrapper fields this time no error but code coverage is showing remains same.

        empW .OppID = opp4.id; 
        empW .productcode = 'FA311';
         empW.compareTo(empW);  



Thanks,
Naveen
Kumar GKumar G
Hi Vara,

Any idea on this.



Thanks..