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
Masechaba Maseli 8Masechaba Maseli 8 

Help with batchable class test

Hi all

I have a batch class that I am gettin​g very litte test coverage for, can someone please assist. Lines 9 to 18 are not being covered.
global class ragStatusUpdate implements Database.Batchable<sObject>{

   global Database.QueryLocator start(Database.BatchableContext BC)

  {   string query = 'select id, Update_Rag_Status_Time__c from Shipment_Order__c  where Shipping_Status__c != \'POD Received\' AND Shipping_Status__c != \'On Hold\'AND Shipping_Status__c != \'Cancelled\'AND Shipping_Status__c != \'Shipment Pending\' AND Shipping_Status__c != \'Cancelled - With fees/costs\' AND Shipping_Status__c != \'Cancelled\'AND Shipping_Status__c != \'Cancelled - No fees/costs\' AND Shipping_Status__c != \'Customs Clearance Docs Received\' ';
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<Shipment_Order__c>scope )
   {
list<Shipment_Order__c> SO = new list<Shipment_Order__c>();
        for(Shipment_Order__c s : scope)
           {
            s.Update_Rag_Status_Time__c  = TRUE;
            
             SO.add(s);
            }      
          update SO;
     }

   global void finish(Database.BatchableContext BC){

   }

}
 
@isTest
private class RagStatusTest {


static testMethod void testMethod1() {
    
        
        Account testClient = new account(Name='TestingClient',Client_Status__c = 'Prospect',Estimated_Vat__c = 123,CSE_IOR__c='0050Y000001LTZO');

        insert testClient;

        Contact testcontact = new Contact(LastName='Maseli',AccountId=testClient.Id);

        insert testcontact;
        
        country_price_approval__c cpa = new country_price_approval__c( name ='Serbia', Billing_term__c = 'DAP/CIF - IOR pays',  Airwaybill_Instructions__c = 'User IOR Address');
        insert cpa;
        
        IOR_Price_List__c testIOR = new IOR_Price_List__c();

        testIOR.Client_Name__c = testClient.Id;
        testIOR.Name = 'Serbia';
        testIOR.IOR_Fee__c = 1;
        
        insert testIOR;

        Shipment_Order__c shipmentOrder = new Shipment_Order__c();
        shipmentOrder.Account__c = testClient.Id;
        shipmentOrder.IOR_Price_List__c = testIOR.Id;
        shipmentOrder.Client_Contact_for_this_Shipment__c = testcontact.Id;
        shipmentOrder.Shipment_Value_USD__c = 123;
        shipmentOrder.Who_arranges_International_courier__c ='Client'; 
        shipmentOrder.Tax_Treatment__c ='DAP/CIF - IOR pays';
        shipmentOrder.Ship_to_Country__c = cpa.Id;
        shipmentOrder.Update_Rag_Status_Time__c = TRUE;
        
        insert shipmentOrder;

        
        
        Test.startTest();

        ragStatusUpdate batch = new ragStatusUpdate(); 
        database.executebatch(batch);
       
          
          

          Test.stopTest();
      }

    }

 
Best Answer chosen by Masechaba Maseli 8
Amit Chaudhary 8Amit Chaudhary 8
Please add Shipping_Status__c  in you test class like below
@isTest
private class RagStatusTest {


static testMethod void testMethod1() {
    
        
        Account testClient = new account(Name='TestingClient',Client_Status__c = 'Prospect',Estimated_Vat__c = 123,CSE_IOR__c='0050Y000001LTZO');

        insert testClient;

        Contact testcontact = new Contact(LastName='Maseli',AccountId=testClient.Id);

        insert testcontact;
        
        country_price_approval__c cpa = new country_price_approval__c( name ='Serbia', Billing_term__c = 'DAP/CIF - IOR pays',  Airwaybill_Instructions__c = 'User IOR Address');
        insert cpa;
        
        IOR_Price_List__c testIOR = new IOR_Price_List__c();

        testIOR.Client_Name__c = testClient.Id;
        testIOR.Name = 'Serbia';
        testIOR.IOR_Fee__c = 1;
        
        insert testIOR;

        Shipment_Order__c shipmentOrder = new Shipment_Order__c();
        shipmentOrder.Account__c = testClient.Id;
        shipmentOrder.IOR_Price_List__c = testIOR.Id;
        shipmentOrder.Client_Contact_for_this_Shipment__c = testcontact.Id;
        shipmentOrder.Shipment_Value_USD__c = 123;
        shipmentOrder.Who_arranges_International_courier__c ='Client'; 
        shipmentOrder.Tax_Treatment__c ='DAP/CIF - IOR pays';
        shipmentOrder.Ship_to_Country__c = cpa.Id;
        shipmentOrder.Update_Rag_Status_Time__c = TRUE;
        shipmentOrder.Shipping_Status__c  ='Test';
        
        insert shipmentOrder;

        
        
        Test.startTest();

        ragStatusUpdate batch = new ragStatusUpdate(); 
        database.executebatch(batch);
       
          
          

          Test.stopTest();
      }

    }

 

All Answers

Aman MalikAman Malik
Hi,

The execute method of your Batchable is only called if the query in the start method returns records. So try to debug the number of records in your test class.
Through this debug,  you will get to know what is missing in your test data.

Thanks,
Aman
Amit Chaudhary 8Amit Chaudhary 8
Please add Shipping_Status__c  in you test class like below
@isTest
private class RagStatusTest {


static testMethod void testMethod1() {
    
        
        Account testClient = new account(Name='TestingClient',Client_Status__c = 'Prospect',Estimated_Vat__c = 123,CSE_IOR__c='0050Y000001LTZO');

        insert testClient;

        Contact testcontact = new Contact(LastName='Maseli',AccountId=testClient.Id);

        insert testcontact;
        
        country_price_approval__c cpa = new country_price_approval__c( name ='Serbia', Billing_term__c = 'DAP/CIF - IOR pays',  Airwaybill_Instructions__c = 'User IOR Address');
        insert cpa;
        
        IOR_Price_List__c testIOR = new IOR_Price_List__c();

        testIOR.Client_Name__c = testClient.Id;
        testIOR.Name = 'Serbia';
        testIOR.IOR_Fee__c = 1;
        
        insert testIOR;

        Shipment_Order__c shipmentOrder = new Shipment_Order__c();
        shipmentOrder.Account__c = testClient.Id;
        shipmentOrder.IOR_Price_List__c = testIOR.Id;
        shipmentOrder.Client_Contact_for_this_Shipment__c = testcontact.Id;
        shipmentOrder.Shipment_Value_USD__c = 123;
        shipmentOrder.Who_arranges_International_courier__c ='Client'; 
        shipmentOrder.Tax_Treatment__c ='DAP/CIF - IOR pays';
        shipmentOrder.Ship_to_Country__c = cpa.Id;
        shipmentOrder.Update_Rag_Status_Time__c = TRUE;
        shipmentOrder.Shipping_Status__c  ='Test';
        
        insert shipmentOrder;

        
        
        Test.startTest();

        ragStatusUpdate batch = new ragStatusUpdate(); 
        database.executebatch(batch);
       
          
          

          Test.stopTest();
      }

    }

 
This was selected as the best answer
Masechaba Maseli 8Masechaba Maseli 8
@Amit Chaudhary 8 

I did exactly that and got 100% coverage earlier. Thank you for the assistance everyone.