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
Ammar P 9Ammar P 9 

How can I write test class with 80 % code coverage? Pls Help me anyone.. Im a Salesforce beginner .

Apex Class:
public class Activelineitem {
    @AuraEnabled
    Public static void ActiveProduct(String oppId, String ProductId, Integer leasePeriod, Integer productQuantity, Decimal AdminSlab, OpportunityLineItem oli, List<OpportunityLineItem> oliList){
        List<OpportunityLineItem> updateOpplineItem = new List<OpportunityLineItem>();
        system.debug('leasePeriod from main class--->>'+leasePeriod);
        List<Opportunity> opplst = [Select AccountId from opportunity where id =: oppId];
        List<Special_Pricing__c> spclList = [Select id,name,Valid_thru__c,AccountLookup__c,Opportunity_Product__r.Product2Id,Approval_status__c
                                             From Special_Pricing__c where AccountLookup__c =: opplst[0].AccountId LIMIT 100];
        system.debug('spclList--->>>>'+spclList);
        //Check for valid special pricing record
        List<Special_Pricing__c> specialPricingRec = new List<Special_Pricing__c>();
        for(Special_Pricing__c spcl : spclList){
            if(spcl.Opportunity_Product__r.Product2Id == ProductId && spcl.Approval_status__c=='Approved by CFO'){
               
                if(spcl.Valid_thru__c == null){
                    specialPricingRec.add(spcl);
                    system.debug('Active product-->'+spcl.Name);
                }
            }
        }
        system.debug('specialPricingRec-->'+specialPricingRec);
        List<Special_Pricing_Set__c> spSetList = new List<Special_Pricing_Set__c>();
        if(specialPricingRec.size() > 0){
            spSetList = [Select id, name,Is_Activecorrect__c From Special_Pricing_Set__c Where Special_Pricing__c=:specialPricingRec[0].id AND Is_Activecorrect__c=:True];
        }
         system.debug('spSetList-->'+spSetList);
        if(spSetList.size() > 0){
            String BreakEvenRecordId = fetchSPBreakEvenDetails(ProductId,AdminSlab,leasePeriod,spSetList[0].id);
            system.debug('BreakEvenRecordId--->>'+BreakEvenRecordId);
            if(!String.isEmpty(BreakEvenRecordId)){
                if(leasePeriod == 1){
                    oli.SP_Break_Even_Ist_year__c = BreakEvenRecordId;
                    oli.SP_Break_Even_2nd_year__c = NULL;
                    oli.SP_Break_Even_3rd_year__c = NULL;
                    oli.SP_Break_Even_4th_year__c = NULL;
                    oli.SP_Break_Even_5th_year__c = NULL;
                   
                }
                if(leasePeriod == 2){
                    oli.SP_Break_Even_Ist_year__c = NULL;
                    oli.SP_Break_Even_2nd_year__c = BreakEvenRecordId;
                    oli.SP_Break_Even_3rd_year__c = NULL;
                    oli.SP_Break_Even_4th_year__c = NULL;
                    oli.SP_Break_Even_5th_year__c = NULL;
                   
                }
                if(leasePeriod == 3){
                    oli.SP_Break_Even_Ist_year__c = NULL;
                    oli.SP_Break_Even_2nd_year__c = NULL;
                    oli.SP_Break_Even_3rd_year__c = BreakEvenRecordId;
                    oli.SP_Break_Even_4th_year__c = NULL;
                    oli.SP_Break_Even_5th_year__c = NULL;
                   
                }
                if(leasePeriod == 4){
                    oli.SP_Break_Even_Ist_year__c = NULL;
                    oli.SP_Break_Even_2nd_year__c = NULL;
                    oli.SP_Break_Even_3rd_year__c = NULL;
                    oli.SP_Break_Even_4th_year__c = BreakEvenRecordId;
                    oli.SP_Break_Even_5th_year__c = NULL;
                   
                }
                if(leasePeriod == 5){
                    oli.SP_Break_Even_Ist_year__c = NULL;
                    oli.SP_Break_Even_2nd_year__c = NULL;
                    oli.SP_Break_Even_3rd_year__c = NULL;
                    oli.SP_Break_Even_4th_year__c = NULL;
                    oli.SP_Break_Even_5th_year__c = BreakEvenRecordId;
                   
                }
                oli.Special_Pricing_Applied__c = true;
                updateOpplineItem.add(oli);
                update oli;
                OpportunityLineItemHelper1.insertOpportunityLineItem(updateOpplineItem);
            }
           
        }
       
        //Update Accessories
        Map<String, Special_Pricing_Tier_Set__c> oliSpMap = new Map<String, Special_Pricing_Tier_Set__c>();
        Id recordTypeId = Schema.SObjectType.Special_Pricing_Tier_set__c.getRecordTypeInfosByName().get('Accessories').getRecordTypeId();
        //system.debug('oliList -->>'+oliList);
        if(oliList.size() > 0 && spSetList.size() > 0){
            for(OpportunityLineItem oliItem : oliList){
                Special_Pricing_Tier_Set__c spt = new Special_Pricing_Tier_Set__c();
                spt.RecordTypeId = recordTypeId;
                spt.Product__c = ProductId;
                spt.Special_Pricing_Set__c = spSetList[0].id;
                spt.One_year__c = TRUE;
                spt.Two_year__c = TRUE;
                spt.Three_year__c = TRUE;
                spt.Four_year__c = TRUE;
                spt.Five_year__c = TRUE;
                spt.Accessories_Name__c = oliItem.Accessories__r.Accessories_Name__c;
                spt.Cost_per_month__c = oliItem.Accessories__r.Cost_per_month__c;
                spt.Monthly_Charge__c = oliItem.Accessories__r.Monthly_Charge__c;
                oliSpMap.put(oliItem.Id, spt);
            }
            system.debug('oliSpMap -->>'+oliSpMap);
            try{
                insert oliSpMap.values();
            }
            catch(Exception e){
                system.debug('Exception--->>'+e.getMessage());
            }
            //system.debug('oliSpMap===>>'+oliSpMap);
            Map<String,OpportunityLineItem> oliMap = new Map<String,OpportunityLineItem>();
            for(OpportunityLineItem oliItem : [Select Id, Name,Break_Even_1st_year__c,Break_Even_2nd_year__c,Break_Even_3rd_year__c,
                                               Break_Even_4th_year__c,Break_Even_5th_year__c,Accessories__c,Lease_Period__c
                                               From OpportunityLineItem Where Id IN:oliSpMap.keySet()]){
                                                   oliMap.put(oliItem.Id, oliItem);
                                               }
            List<OpportunityLineItem> updateOli = new List<OpportunityLineItem>();
            for(String oliVal : oliSpMap.keySet()){
                OpportunityLineItem oliUpdate = new OpportunityLineItem();
                oliUpdate = oliMap.get(oliVal);
                oliUpdate.Accessories_Sp__c = oliSpMap.get(oliVal).Id;
               
                // add lease amount
                if(oliUpdate.Lease_Period__c == '1'){                    
                    oliUpdate.SP_Break_Even_Ist_year__c = oliSpMap.get(oliVal).Id;
                    oliUpdate.SP_Break_Even_2nd_year__c = NULL;
                    oliUpdate.SP_Break_Even_3rd_year__c = NULL;
                    oliUpdate.SP_Break_Even_4th_year__c = NULL;
                    oliUpdate.SP_Break_Even_5th_year__c = NULL;                    
                   
                }
                if(oliUpdate.Lease_Period__c == '2'){
                    oliUpdate.SP_Break_Even_Ist_year__c = NULL;
                    oliUpdate.SP_Break_Even_2nd_year__c = NULL;
                    oliUpdate.SP_Break_Even_3rd_year__c = oliSpMap.get(oliVal).Id;
                    oliUpdate.SP_Break_Even_4th_year__c = NULL;
                    oliUpdate.SP_Break_Even_5th_year__c = NULL;
                   
                }
                if(oliUpdate.Lease_Period__c == '3'){
                    oliUpdate.SP_Break_Even_Ist_year__c = NULL;
                    oliUpdate.SP_Break_Even_2nd_year__c = NULL;
                    oliUpdate.SP_Break_Even_3rd_year__c = oliSpMap.get(oliVal).Id;
                    oliUpdate.SP_Break_Even_4th_year__c = NULL;
                    oliUpdate.SP_Break_Even_5th_year__c = NULL;
                   
                }
                if(oliUpdate.Lease_Period__c == '4'){
                    oliUpdate.SP_Break_Even_Ist_year__c = NULL;
                    oliUpdate.SP_Break_Even_2nd_year__c = NULL;
                    oliUpdate.SP_Break_Even_3rd_year__c = NULL;
                    oliUpdate.SP_Break_Even_4th_year__c = oliSpMap.get(oliVal).Id;
                    oliUpdate.SP_Break_Even_5th_year__c = NULL;
                   
                }
                if(oliUpdate.Lease_Period__c == '5'){
                    oliUpdate.SP_Break_Even_Ist_year__c = NULL;
                    oliUpdate.SP_Break_Even_2nd_year__c = NULL;
                    oliUpdate.SP_Break_Even_3rd_year__c = NULL;
                    oliUpdate.SP_Break_Even_4th_year__c = NULL;
                    oliUpdate.SP_Break_Even_5th_year__c = oliSpMap.get(oliVal).Id;
                   
                }
                oliUpdate.Special_Pricing_Applied__c = true;
                updateOli.add(oliUpdate);
            }
            try{
                update updateOli;
            }
            catch(Exception e){
                system.debug('Exception -->>'+e.getMessage());
            }
            OpportunityLineItemHelper1.insertOpportunityLineItem(updateOli);
        }
    }
   
   
    @AuraEnabled
    Public static String fetchSPBreakEvenDetails(String ProductId, Decimal AdminSlab, Integer leasePeriod, String spSetId){
        system.debug('ProductId-->>'+ProductId);
        system.debug('AdminSlab-->>'+AdminSlab);
        system.debug('leasePeriod-->>'+leasePeriod);
        system.debug('spSetId-->>'+spSetId);
        if(leasePeriod == 1){
            List<Special_Pricing_Tier_Set__c> spriceTierSetList = new List<Special_Pricing_Tier_Set__c>();
            spriceTierSetList = [Select Id, Name From Special_Pricing_Tier_Set__c Where Special_Pricing_Set__r.Special_Pricing__r.Opportunity_Product__r.Product2Id =:ProductId AND Special_Pricing_Set__c=:spSetId
                                 AND One_year__c=TRUE AND Two_year__c=FALSE AND Three_year__c=FALSE AND Four_year__c=FALSE
                                 AND Five_year__c=FALSE LIMIT 1];
            if(spriceTierSetList.size() >0 ){
                return spriceTierSetList[0].id;
            }
            else{
                return '';
            }
        }
        else if(leasePeriod == 2){
            List<Special_Pricing_Tier_Set__c> spriceTierSetList = new List<Special_Pricing_Tier_Set__c>();
            spriceTierSetList = [Select Id, Name From Special_Pricing_Tier_Set__c Where Special_Pricing_Set__r.Special_Pricing__r.Opportunity_Product__r.Product2Id =:ProductId AND Special_Pricing_Set__c=:spSetId AND One_year__c=TRUE AND
                                 Two_year__c=TRUE AND Three_year__c=FALSE AND Four_Year__c=FALSE AND Five_year__c=FALSE LIMIT 1];
           
            if(spriceTierSetList.size() >0 ){
                return spriceTierSetList[0].id;
            }
            else{
                return '';
            }
        }
        else if(leasePeriod == 3){
            List<Special_Pricing_Tier_Set__c> spriceTierSetList = new List<Special_Pricing_Tier_Set__c>();
            spriceTierSetList = [Select Id, Name From Special_Pricing_Tier_Set__c Where Special_Pricing_Set__r.Special_Pricing__r.Opportunity_Product__r.Product2Id =:ProductId AND Special_Pricing_Set__c=:spSetId AND One_year__c=TRUE AND
                                 Two_year__c=TRUE AND Three_year__c=TRUE AND Four_Year__c=FALSE AND Five_Year__c=FALSE LIMIT 1];
           
            if(spriceTierSetList.size() >0 ){
                return spriceTierSetList[0].id;
            }
            else{
                return '';
            }
        }
        else if(leasePeriod == 4){
            List<Special_Pricing_Tier_Set__c> spriceTierSetList = new List<Special_Pricing_Tier_Set__c>();
            spriceTierSetList = [Select Id, Name From Special_Pricing_Tier_Set__c Where Special_Pricing_Set__r.Special_Pricing__r.Opportunity_Product__r.Product2Id =:ProductId AND Special_Pricing_Set__c=:spSetId AND One_year__c=TRUE AND
                                 Two_year__c=TRUE AND Three_year__c=TRUE AND Four_Year__c=TRUE AND Five_Year__c=FALSE LIMIT 1];
           
            if(spriceTierSetList.size() >0 ){
                return spriceTierSetList[0].id;
            }
            else{
                return '';
            }
        }
        else if(leasePeriod == 5){
            List<Special_Pricing_Tier_Set__c> spriceTierSetList = new List<Special_Pricing_Tier_Set__c>();
            spriceTierSetList = [Select Id, Name From Special_Pricing_Tier_Set__c Where Special_Pricing_Set__r.Special_Pricing__r.Opportunity_Product__r.Product2Id =:ProductId AND Special_Pricing_Set__c=:spSetId AND One_year__c=TRUE AND
                                 Two_year__c=TRUE AND Three_year__c=TRUE AND Four_Year__c=TRUE AND Five_Year__c=TRUE LIMIT 1];
           
            if(spriceTierSetList.size() >0 ){
                return spriceTierSetList[0].id;
            }
            else{
                return '';
            }
        }
        else{
            return '';
        }
    }
}
SwethaSwetha (Salesforce Developers) 
The code provided in question does not highlight the uncovered lines in bold. Since this code is huge and requires an understanding of your implementation, it might not be possible to provide exact edit suggestions. However, the below articles give a good insight into how coverage can be improved
 
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
 
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test
 
https://salesforce.stackexchange.com/questions/167251/test-class-with-auraenabled-methods-in-apex-class/167252

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you