+ Start a Discussion
Mahesh Babu 187Mahesh Babu 187 

Test Class for a class which is called in a Trigger

Hi Team,

We have a Apex class which is called in a trigger. I have writen a test class for that Apex class but my code coverage is only 5%. Only first 2 lines are getting covered. I have tried multiple approaches but all in vain. Please assist me to do the changes.

APEX CODE:

public class BQDetailsUpdateClass {

    public static void createBQDetails(Product2 product){
               if(product.Vertical__c != null && product.Vertical__c.equals('Credit Cards') && product.Approval_Status__c != null && product.Approval_Status__c.equals('Approved')) {
                List<PriceBookEntry> pbes = [Select Id,UnitPrice,CurrencyIsoCode,BQ_Detail__c from PriceBookEntry where Product2Id =: product.Id and Pricebook2Id != '01s41000007uZohAAE' and IsActive = true];
                for(PriceBookEntry pbe : pbes) {
                    BQ_Detail__c bqd = new BQ_Detail__c();
                    if(product.Lightbox_Pricing__c) {
                        bqd.Product__c = product.Id;
                        bqd.Vantage_Pricing__c = true;
                        bqd.Price__c = pbe.UnitPrice;
                        bqd.CurrencyIsoCode = pbe.CurrencyIsoCode;
                    } else {
                        bqd.Product__c = product.Id;
                        bqd.Price__c = pbe.UnitPrice;
                        bqd.CurrencyIsoCode = pbe.CurrencyIsoCode;
                    }
                    bqd.Eligible_for_BQ__c = true;
                    if(String.IsBlank(pbe.BQ_Detail__c)) {
                        insert bqd;
                        pbe.BQ_Detail__c = bqd.Id;
                        update pbe;
                    } else {
                        bqd.Id = pbe.BQ_Detail__c;
                        update bqd;
                    }
                    
                }
                List<Product_Offering__c> poffers = [Select Id ,CurrencyIsoCode, Offer_Name__c ,BQ_Detail__c,List_Price__c, End_Date__c, Start_Date__c from Product_Offering__c where Product__c =: product.Id and Active__c = true];
                
                for(Product_Offering__c poffer : poffers) {
                    BQ_Detail__c bqd = new BQ_Detail__c();
                    bqd.Product__c = product.Id;
                    bqd.Price__c = poffer.List_Price__c;
                    bqd.CurrencyIsoCode = poffer.CurrencyIsoCode;
                    bqd.Offer_Name__c = poffer.Offer_Name__c;
                    bqd.Start_Date__c = poffer.Start_Date__c;
                    bqd.End_Date__c = poffer.Start_Date__c;
                    bqd.Eligible_for_BQ__c = true;
                    if(String.IsBlank(poffer.BQ_Detail__c)) {
                        insert bqd;
                        poffer.BQ_Detail__c = bqd.Id;
                        update poffer;
                    } else {
                        bqd.Id = poffer.BQ_Detail__c;
                        update bqd;
                    }
                }
              }    
}

TEST CLASS:

@isTest(SeeAllData=true)
public class BQDetailsUpdateClassTest {
    
    @isTest
    public static void Test1(){
        
            Product2 prod = new Product2();
            PricebookEntry standardPrice = new PricebookEntry();
            BQ_Detail__c bqd1 = new BQ_Detail__c();        
            bqd1.Product__c=prod.Id;        
            bqd1.Price__c=standardPrice.UnitPrice;
            bqd1.Vantage_Pricing__c=true;
            bqd1.CurrencyIsoCode=standardPrice.CurrencyIsoCode;
            insert bqd1;      
       

    //PricebookEntry updatePriceBookEntry = new PricebookEntry( id = standardPrice.Id,UnitPrice = 30.00);
    //update updatePriceBookEntry;    
       
       /*public static void  test2(){
            BQ_Detail__c bqd = new BQ_Detail__c();
            Product2 prod = new Product2();
              PricebookEntry pbes = new PricebookEntry();
           bqd.Id=pbes.BQ_Detail__c;
           update bqd; 
        }*/
    
   /* @isTest
     public static void Test2(){
      Product2 prod = new Product2();
     PricebookEntry standardPrice = new PricebookEntry();
            BQ_Detail__c bqd2 = new BQ_Detail__c();
        
            bqd2.Product__c=prod.Id;
        
        bqd2.Price__c=standardPrice.UnitPrice;
        bqd2.Vantage_Pricing__c=true;
        bqd2.CurrencyIsoCode=standardPrice.CurrencyIsoCode;
        insert bqd2; 
*/              
       
       BQDetailsUpdateClass.createBQDetails(prod);
        
    } 
    
          /*Pricebook2 customPB = new Pricebook2(
          Name='Custom Pricebook',
          isActive=true);
        insert customPB;*/   
      
        /*PricebookEntry pbes = new PricebookEntry(
            Pricebook2Id = customPB.Id, 
            Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert pbes;
        
  
        List<PricebookEntry> pbes1 = [SELECT ID, Product2Id, 
        Pricebook2.isStandard, Pricebook2.isActive, isActive FROM PricebookEntry];*/   
    
        /*Id pricebookId = Test.getStandardPricebookId();
        Product2 p =new Product2();
        p.Name = 'Test';
        p.Description = 'Test';
        p.Vertical__c = 'Credit Cards';
        p.Lightbox_Pricing__c = false;
        p.Location__c = '100 - SF 760 Market St';
        p.CurrencyIsoCode='CAD - Canadian Dollar';
        insert p;
        List<PriceBookEntry> pbes = [Select Id,UnitPrice,CurrencyIsoCode,BQ_Detail__c from PriceBookEntry where Product2Id =: p.Id];
     PriceBookEntry pbe = new PriceBookEntry();
     pbe.UnitPrice = 5;
      pbe.Pricebook2Id = '01s02000000xDR3AAM';
     insert pbe;  
        
    }*/      
}

Thanks,
Mahesh
Santosh Kumar 348Santosh Kumar 348
Hi Mahesh,

As you have mentioned that only 2 lines are getting covered. Looking at your code I assume coverage is not going beyonbd below condition.
if(product.Vertical__c != null && product.Vertical__c.equals('Credit Cards') && product.Approval_Status__c != null && product.Approval_Status__c.equals('Approved')) {

If this is the case I would like to highlight few things in your code.
  1. First of all remove @isTest(SeeAllData=true) from your test class. This is not the best practicse.
  2. I can see you have already written the code and then commented it. So remove @isTest(SeeAllData=true) and uncomment the code.
  3. The code is not going beyond this line is because conditions are not getting satisfied. You have Approval_Status__c in "&&" condition, so would suggest you to add this value in you test class code for product.
p.Approval_Status__c = 'Approved';

Then try it will go inside the condition.

If it helped then can you please mark it as the best answer so that it can be used by others in the future.

Regards,
Santosh Kumar


 
Mahesh Babu 187Mahesh Babu 187
Hi Santosh,
If I remove @isTest(SeeAllData=true), then it is throwing error. i have modified the code now but the coverage is 16% only.

TEST CLASS CODE

@isTest(SeeAllData=True)
public class BQDetailsUpdateClassTest_PK {
@isTest
    static void createBQDetailsTest1()
    {
      
        Product2 prod = new Product2();
        prod.Name = 'Test ck';
        prod.Description = 'Test';
        prod.Vertical__c = 'Credit Cards';
        prod.Lightbox_Pricing__c = true;
        prod.IsActive = true;
        prod.Approval_Status__c='Approved';
        prod.Activated_Start_Date__c=System.today();
        prod.Activated_End_Date__c=System.today();
        insert prod;
        
       Pricebook2 standard = [Select Id, Name, IsActive From Pricebook2 where IsStandard = true LIMIT 1];
       Id pricebookId = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = standard.Id, 
            Product2Id = prod.Id,
            UnitPrice = 10, 
            IsActive = true);
        insert standardPrice;
        
        Product_Offering__c po= new Product_Offering__c();
        po.List_Price__c=26;
        po.CurrencyIsoCode=prod.CurrencyIsoCode;
        insert po;     

    }
    
    @isTest
    public static void Test1(){
       Product2 prod = new Product2();
       PricebookEntry standardPrice = new PricebookEntry();
            BQ_Detail__c bqd1 = new BQ_Detail__c();        
            bqd1.Product__c=prod.Id;        
           bqd1.Price__c=standardPrice.UnitPrice;
           bqd1.Vantage_Pricing__c=true;
           bqd1.CurrencyIsoCode=standardPrice.CurrencyIsoCode;
          insert bqd1; 
        
         //PricebookEntry updatePriceBookEntry = new PricebookEntry( id=standardPrice.Id,UnitPrice = 30.00);
    //update updatePriceBookEntry;
       
       BQDetailsUpdateClass.createBQDetails(prod);

}
     @isTest
     public static void Test2(){
         Product2 prod = new Product2();
         PricebookEntry standardPrice = new PricebookEntry();
         Product_Offering__c po= new Product_Offering__c();
         BQ_Detail__c bqd2 = new BQ_Detail__c();
         bqd2.Product__c=prod.Id;
         bqd2.Price__c=po.List_Price__c;
         bqd2.CurrencyIsoCode = po.CurrencyIsoCode;
         bqd2.Eligible_for_BQ__c = true;
         BQDetailsUpdateClass.createBQDetails(prod);         
     }    
}

Please assist me to increase my code coverage further.
Mahesh Babu 187Mahesh Babu 187
Lines Covered are:
Test class

 
Mahesh Babu 187Mahesh Babu 187
Hi Team,
 
I have tried to modify my test class but still I my code coverage is 15%.

TEST CLASS:

@isTest(SeeAllData=True)
public class BQDetailsUpdateClassTest_PK {
    
    @isTest
    public static void lightboxTrue(){
        Product2 prod = new Product2(); 
        prod.Name = 'Test ck';
        prod.Description = 'Test';
        prod.Vertical__c = 'Credit Cards';
        prod.Lightbox_Pricing__c = true;
        prod.IsActive = true;
        prod.Approval_Status__c='Approved';
        prod.Activated_Start_Date__c=System.today();
        prod.Activated_End_Date__c=System.today();
        insert prod;  
        PricebookEntry pbe = new PricebookEntry();
        BQ_Detail__c bqd1 = new BQ_Detail__c();        
        bqd1.Product__c=prod.Id;        
        bqd1.Price__c=pbe.UnitPrice;
        bqd1.Vantage_Pricing__c=true;
        bqd1.CurrencyIsoCode=pbe.CurrencyIsoCode;
        insert bqd1; 
        
        //PricebookEntry updatePriceBookEntry = new PricebookEntry( id=standardPrice.Id,UnitPrice = 30.00);
        //update updatePriceBookEntry;
        
        BQDetailsUpdateClass.createBQDetails(prod);
        
    }
    
    @isTest
    public static void lightboxFalse(){
        Product2 prod = new Product2(); 
        prod.Name = 'Test ck';
        prod.Description = 'Test';
        prod.Vertical__c = 'Credit Cards';
        prod.Lightbox_Pricing__c = false;
        prod.IsActive = true;
        prod.Approval_Status__c='Approved';
        prod.Activated_Start_Date__c=System.today();
        prod.Activated_End_Date__c=System.today();
        insert prod;  
        PricebookEntry pbe = new PricebookEntry();
        BQ_Detail__c bqd1 = new BQ_Detail__c();        
        bqd1.Product__c=prod.Id;        
        bqd1.Price__c=pbe.UnitPrice;
        bqd1.CurrencyIsoCode=pbe.CurrencyIsoCode;
        insert bqd1; 
        
        //PricebookEntry updatePriceBookEntry = new PricebookEntry( id=standardPrice.Id,UnitPrice = 30.00);
        //update updatePriceBookEntry;
        
        BQDetailsUpdateClass.createBQDetails(prod);
        
    }
    
    
    
    @isTest
    public static void Test2(){
        Product2 prod = new Product2();
        prod.Name = 'Test ck';
        prod.Description = 'Test';
        prod.Vertical__c = 'Credit Cards';
        prod.Lightbox_Pricing__c = true;
        prod.IsActive = true;
        prod.Approval_Status__c='Approved';
        prod.Activated_Start_Date__c=System.today();
        prod.Activated_End_Date__c=System.today();
        insert prod;
        PricebookEntry pbe = new PricebookEntry();
        Product_Offering__c po= new Product_Offering__c();
        po.List_Price__c=26;
        po.CurrencyIsoCode=prod.CurrencyIsoCode;
        po.BQ_Detail__c ='a1v02000000Nril';
        insert po;
        BQ_Detail__c bqd2 = new BQ_Detail__c();
        bqd2.Product__c=prod.Id;
        bqd2.Price__c=po.List_Price__c;
        bqd2.Offer_Name__c = po.Offer_Name__c;
        bqd2.CurrencyIsoCode = po.CurrencyIsoCode;
        bqd2.Start_Date__c = po.Start_Date__c;
        bqd2.End_Date__c = po.End_Date__c;
        bqd2.Eligible_for_BQ__c = true;
        insert bqd2;
        BQDetailsUpdateClass.createBQDetails(prod);
        
    }
    
    @isTest
    public static void updatePricebook(){
        Product2 prod = new Product2();
        prod.Name = 'Test ck';
        prod.Description = 'Test';
        prod.Vertical__c = 'Credit Cards';
        prod.Lightbox_Pricing__c = true;
        prod.IsActive = true;
        prod.Approval_Status__c='Approved';
        prod.Activated_Start_Date__c=System.today();
        prod.Activated_End_Date__c=System.today();
        insert prod;
        PricebookEntry pbe = new PricebookEntry();
        pbe.UnitPrice = 2;
        pbe.Product2Id = prod.id;
        pbe.Pricebook2Id = '01s41000007uZohAAE';
        insert pbe;
        BQ_Detail__c bqd2 = new BQ_Detail__c();
        bqd2.Start_Date__c = prod.Activated_Start_Date__c;
        bqd2.End_Date__c = prod.Activated_End_Date__c;
        bqd2.Eligible_for_BQ__c = true;
        pbe.BQ_Detail__c = null;
        insert bqd2;
        pbe.BQ_Detail__c = bqd2.Id;
        update pbe;
        BQDetailsUpdateClass.createBQDetails(prod);
        
    }
    
    @isTest
    public static void updateBqd1(){
        Product2 prod = new Product2();
        prod.Name = 'Test ck';
        prod.Description = 'Test';
        prod.Vertical__c = 'Credit Cards';
        prod.Lightbox_Pricing__c = true;
        prod.IsActive = true;
        prod.Approval_Status__c='Approved';
        prod.Activated_Start_Date__c=System.today();
        prod.Activated_End_Date__c=System.today();
        insert prod;
        PricebookEntry pbe = new PricebookEntry();
        pbe.UnitPrice = 2;
        pbe.Product2Id = prod.id;
        pbe.Pricebook2Id = '01s41000007uZohAAE';
        insert pbe;
        BQ_Detail__c bqd2 = new BQ_Detail__c();
        bqd2.Start_Date__c = prod.Activated_Start_Date__c;
        bqd2.End_Date__c = prod.Activated_End_Date__c;
        bqd2.Eligible_for_BQ__c = true;
        pbe.BQ_Detail__c = 'a1v02000000NWXoAAO';
        insert bqd2;
        bqd2.Id= pbe.BQ_Detail__c;
        update bqd2;
        BQDetailsUpdateClass.createBQDetails(prod);
        
    }
    
    @isTest
    public static void updateProductoffer(){
        Product2 prod = new Product2();
        prod.Name = 'Test ck';
        prod.Description = 'Test';
        prod.Vertical__c = 'Credit Cards';
        prod.Lightbox_Pricing__c = true;
        prod.IsActive = true;
        prod.Approval_Status__c='Approved';
        prod.Activated_Start_Date__c=System.today();
        prod.Activated_End_Date__c=System.today();
        insert prod;
        Product_Offering__c po= new Product_Offering__c();
        po.List_Price__c=26;
        po.CurrencyIsoCode=prod.CurrencyIsoCode;
        po.BQ_Detail__c ='a1v02000000Nril';
        insert po; 
        BQ_Detail__c bqd2 = new BQ_Detail__c();
        bqd2.Product__c = prod.id;
        bqd2.Price__c = po.List_Price__c;
        bqd2.CurrencyIsoCode = po.CurrencyIsoCode;
        bqd2.Offer_Name__c = po.Offer_Name__c;
        bqd2.Start_Date__c = po.Start_Date__c;
        bqd2.End_Date__c = po.End_Date__c;
        bqd2.Eligible_for_BQ__c = true;
        po.BQ_Detail__c = null;
        insert bqd2;
        po.BQ_Detail__c = bqd2.Id;
        update po;
        BQDetailsUpdateClass.createBQDetails(prod);
        
    }
    
    @isTest
    public static void updateBqd2(){
        Product2 prod = new Product2();
        prod.Name = 'Test ck';
        prod.Description = 'Test';
        prod.Vertical__c = 'Credit Cards';
        prod.Lightbox_Pricing__c = true;
        prod.IsActive = true;
        prod.Approval_Status__c='Approved';
        prod.Activated_Start_Date__c=System.today();
        prod.Activated_End_Date__c=System.today();
        insert prod;
        Product_Offering__c po= new Product_Offering__c();
        po.List_Price__c=26;
        po.CurrencyIsoCode=prod.CurrencyIsoCode;
        po.BQ_Detail__c ='a1v02000000Nril';
        insert po; 
        BQ_Detail__c bqd2 = new BQ_Detail__c();
        bqd2.Product__c = prod.id;
        bqd2.Price__c = po.List_Price__c;
        bqd2.CurrencyIsoCode = po.CurrencyIsoCode;
        bqd2.Offer_Name__c = po.Offer_Name__c;
        bqd2.Start_Date__c = po.Start_Date__c;
        bqd2.End_Date__c = po.End_Date__c;
        bqd2.Eligible_for_BQ__c = true;
        insert bqd2;
        bqd2.Id = po.BQ_Detail__c;
        update bqd2;
        BQDetailsUpdateClass.createBQDetails(prod);
        
    }
}

Please help me to increase the code coverage.

Thanks,
Mahesh