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
Jakson MonteiroJakson Monteiro 

Test coverage

Not able to get coverage for the below code in bold.

global class RS_DeleteExportRecords  implements Database.Batchable<sObject>, Schedulable{
   global final String Query;
    global RS_DeleteExportRecords (){
        
    }

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator([select id from RS_Export__c where CreatedDate != Today]);
    }

    global void execute(Database.BatchableContext BC,List<RS_Export__c> scope){
        delete scope;
    }
pconpcon
What tests have you written for it?  Did you call the Database.executeBatch command?  Can you please include the test code that you have written for this?

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
Arun KumarArun Kumar
Hi Jakson,

Try below code:
@isTest
public class UpdateLastShipmentHistoryDateBatch_Test {

    public static testMethod void testMethodBatch()
    { 
		RS_Export__c tempObj = new RS_Export__c(Name = 'test');
		insert tempObj;
		
		RS_DeleteExportRecords instance = new RS_DeleteExportRecords();
        Database.executeBatch(instance);
	}
}

Please let me know if the helps.


As a common practice, if your question is answered, please choose 1 best answer. Additionaly you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Arun
Amit Chaudhary 8Amit Chaudhary 8
Hi,

As per your Batch job you are fatching all "RS_Export__c" record which created date is not today
return Database.getQueryLocator([select id from RS_Export__c where CreatedDate != Today]);
In your test class you can not control the createdDate So please modify your Batch job like below then try below test class.
global class RS_DeleteExportRecords  implements Database.Batchable<sObject>, Schedulable
{
    global final String Query;
    global RS_DeleteExportRecords ()
	{
    }

    global Database.QueryLocator start(Database.BatchableContext BC)
	{
		if(Test.isRunningTest())
		{
			return Database.getQueryLocator([select id from RS_Export__c ]);
		}
		else
		{
			return Database.getQueryLocator([select id from RS_Export__c where CreatedDate != Today]);
		}		
    }

    global void execute(Database.BatchableContext BC,List<RS_Export__c> scope){
        delete scope;
    }
}



Please try below test class.
@isTest
public class UpdateLastShipmentHistoryDateBatch_Test 
{
    public static testMethod void testMethodBatch()
    { 
		RS_Export__c tempObj = new RS_Export__c(Name = 'test');
		// insert all required field here
		insert tempObj;
		
		RS_DeleteExportRecords instance = new RS_DeleteExportRecords();
        Database.executeBatch(instance);
	}
}
Please let us know if this will help you