+ Start a Discussion
Kumar GKumar G 

How to improve code coverage for below hepler class

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.
private class Test_OpportunityLineItemCheck 
  static testMethod void Checkduplicate() 
        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();

Kumar GKumar G
Please find the corresponding helper class below : 

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;
        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{
        public String OppID;
        public String ProductID;
        public 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;     

Tim CaljeTim Calje

Hi Kumar,

If you open the Class using the Developer Console, you will see highlighted sections on your code coverage, giving you a better understanding of which part is covered, and also the parts that require additional testing (e.g. If-Then-Else branches that are not fully tested).

This feature also alows you to display the code coverage per test.

User-added image

Hope this will help you,

Kumar GKumar G
Hi Everyone,

This block of code is not covered in my class , please suggest me how to cover this section.

Uncovered code