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
John Carton 6John Carton 6 

Need help with test class suffering from deficit code coverage

My test class achieved 73% code coverage. Please help me to get the required code coverage.

Class :
 
public class QuoteFieldHelpers 
{
    public static boolean isExecuting=false;
    
    public static void updateValues( Quote q)
    {
        if(QuoteFieldHelpers.isExecuting)
        {
            return;
        }
        
        QuoteFieldHelpers.isExecuting=true;
        
        Quote[] quotes=new Quote[]{};
            
       
            Quote qu=new Quote(Id =q.Id);
        	QuoteLineItem qle;
        try
        {
             qle=[select GRAND_TOTAL__c from QuoteLineItem where QuoteId   =:q.id Limit 1];
        }
        catch(Exception e)
        {
            System.debug('Error at query result'+e.getMessage());
        }
            
             
              if(qle != null)
              {
                  try
                  {
                      System.debug('grant total from QLI : '+qle.GRAND_TOTAL__c);
                  	  qu.TOTAL_AOS_Including_Taxes__c=qle.GRAND_TOTAL__c;
                  	  quotes.add(qu);
                  }
                  catch(Exception e)
                  {
                      System.debug('Error during Grand total fetch'+e.getMessage());
                  }
                  
              }
            try
            {
                update quotes;
            }
            catch(Exception e)
            {
                System.debug('Error during field update quote : '+e.getMessage());
            }
        
        
    }

}
Test Class :
 
@isTest
public class QuoteControlTestClass 
{

public static Quote qu;
public static Opportunity op;
public static QuoteLineItem qle;

    
    public static testMethod void method1()
    {
        List<User> salesExec=[select Id from user];
        Opportunity opp=new Opportunity();
        opp.Name='Cheky Voola';
        opp.Account=new Account(id='0010l00000EhKdL');
        opp.CurrencyIsoCode='INR';
        opp.CloseDate=Date.newInstance(2018, 1, 10);
        opp.StageName='Qualification';
        opp.Rating__c='Hot';
        opp.Preferred_Project__c='Test Project';
        opp.Sales_Executive__c=salesExec.get(1).Id;
        opp.Amenities__c='Pool and Badminton';
        opp.Certifications__c='No certifications';
        opp.Brand__c='Under Armour';
        opp.Preferred_Location__c='Kavali';
        opp.Budget__c='70 Lakhs - 75 Lakhs';
        insert opp;
        Id pricebookId = Test.getStandardPricebookId();
        
        Opportunity o=[select Id,Name from Opportunity where id=:opp.Id];
        Product2 pr=new Product2();
        pr.Name='Test Product';
        pr.IsActive=True;
        pr.CurrencyIsoCode='INR';
        pr.Floor__c='9';
        pr.Total_Charges_including_Amenities_del__c=12345;
        pr.GST_Service_Tax__c=6;
        insert pr;
        Product2 prod=[select Name,IsActive,CurrencyIsoCode,Floor__c,Total_Amount_with_Agreement_of_Sale__c from Product2 where Id=:pr.Id];
        
        
        PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod.Id,UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        Quote q=new Quote(Name='Trent',OpportunityId=o.Id,Pricebook2Id=pricebookId);
        insert q;
        qle=new QuoteLineItem();
        qle.QuoteId=q.Id;
        qle.Product2Id=prod.Id;
        qle.UnitPrice=12345;
        qle.Quantity=1;
        qle.PricebookEntryId=standardPrice.Id;
        insert qle;
        q.TOTAL_AOS_Including_Taxes__c=qle.GRAND_TOTAL__c;
        upsert q;
        QuoteLineItem qle2=[select QuoteId,GRAND_TOTAL__c from QuoteLineItem where product2Id=:prod.Id ];
        Quote quot=[select TOTAL_AOS_Including_Taxes__c from quote where id=:qle2.QuoteId ];
        System.debug('TOTAL_AOS_Including_Taxes__c from quote : '+quot.TOTAL_AOS_Including_Taxes__c);
        System.debug('TOTAL_AOS_Including_Taxes__c : '+quot.TOTAL_AOS_Including_Taxes__c);
        
        
        //System.debug('Total_Amount_with_Agreement_of_Sale__c : '+prod.Total_Amount_with_Agreement_of_Sale__c);
        //op=[select Name from Opportunity limit 1];
        //qle=[select QuoteId,GRAND_TOTAL__c from QuoteLineItem where product2Id=:prod.Id limit 1];
        //Quote quot=[select TOTAL_AOS_Including_Taxes__c from quote where id=:qle.QuoteId limit 1];
        //System.debug('TOTAL_AOS_Including_Taxes__c from quote : '+quot.TOTAL_AOS_Including_Taxes__c);
        //QuoteFieldHelpers.updateValues(q);
        
    }
}

 
David @ ConfigeroDavid @ Configero

First, clean up your code and run your code coverage again.

public class QuoteFieldHelpers {
	public static boolean isExecuting = false;

	public static void updateValues(Quote quote) {
		if (QuoteFieldHelpers.isExecuting) {
			return;
		}

		QuoteFieldHelpers.isExecuting = true;

		Quote quoteToUpdate = new Quote(Id = quote.Id);

		try {
			QuoteLineItem quoteLineItem = [select GRAND_TOTAL__c from QuoteLineItem where QuoteId = :quote.id Limit 1];
			System.debug('grant total from quoteLineItem : ' + quoteLineItem.GRAND_TOTAL__c);
			quoteToUpdate.TOTAL_AOS_Including_Taxes__c = quoteLineItem.GRAND_TOTAL__c;

			update quoteToUpdate;
		} catch (Exception ex) {
			System.debug('Error ' + ex.getMessage());
		}
	}
}
 

Second, read the below suggestions regarding your original code/tests.

This might not be the direct answer you are looking for, but your unit test is not actually testing or validating anything short of complete failure on insert/update of certain records (which doesn't mean much).  You should be validating the expected behavior and conditional logic to ensure it handles all valid/invalid paths correctly.

The intention of unit testing in Apex is not to blindly achieve code coverage, but for code coverage to be an indicator for where you might have missed opportunities to correctly write unit/behavioral tests.  I would take a step back and do some studying on unit testing, then you will easily achieve 75%+ code coverage as a side effect.

you don't need the if conditional on #29 or the try catch block on #31.  You should instead move that block between #33-35 inside the try catch block at #19 after the SOQL query.  Also it is an extremely dangerous and discouraged practice to use catch blocks for all exceptions and ignore the error.  Simplifying the cyclic complexity of your code will yield you a higher code coverage , but again I highly suggest you take the above recommendations to heart and invest some time in yourself to test it better.