I´m trying to run a Batch to delete some 'expired' Cases. In the test method when I execute the Batch the class constructor runs but the batch start method never runs.

The batch class:

global class ISSP_AMC_CaseCleanUpBatch implements Database.Batchable<sObject> {

 private Date dateToDeleteTo;

 global ISSP_AMC_CaseCleanUpBatch(){

    AMC_Configuration__c amcConfiguration = AMC_Configuration__c.getInstance();
    Integer numberOfDays = Integer.valueOf(amcConfiguration.Case_Number_Of_Days_To_Delete_To__c);

    dateToDeleteTo = Date.today() - numberOfDays;

 global Database.QueryLocator start(Database.BatchableContext BC) {
    // get the RecordType Id
    Id recordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Airline Coding Application').getRecordTypeId();

    // Query Case Object
    return Database.getQueryLocator([
            SELECT id
            FROM Case
            WHERE Id IN (
                    SELECT Case__c
                    FROM Participation_Online_Application__c
                    WHERE LastModifiedDate < :dateToDeleteTo
            AND Status = 'Open'
            AND RecordTypeId = :recordTypeId


 global void execute(Database.BatchableContext BC, List<Case> scope) {

    List <Participation_Online_Application__c> participationList = [SELECT Id FROM Participation_Online_Application__c WHERE Case__c IN :scope];
    if ( scope.size() > 0){
        delete scope;

    if ( participationList.size() > 0 ){
        delete participationList;


 global void finish(Database.BatchableContext BC) {

The Test Class part that executes the Batch after the data is created in a Data Factory:

        ISSP_AMC_CaseCleanUpBatch cleanUpBatch = new ISSP_AMC_CaseCleanUpBatch();

        List<Case> batchedCaseList = [SELECT id FROM Case];
        List<Participation_Online_Application__c> batchedParticipationOnlineApplications = [SELECT Id FROM Participation_Online_Application__c ];
        List<Participant_Online_App_Requirement__c> batchedParticipantOnlineAppRequirements = [SELECT Id FROM Participant_Online_App_Requirement__c];

        list<Participation_Online_Application__c> poApp = [select LastModifiedDate from Participation_Online_Application__c];
        System.debug('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> test ' + poApp);

        System.assertEquals( 0 , batchedCaseList.size());
        System.assertEquals( 0 , batchedParticipantOnlineAppRequirements.size());
        System.assertEquals( 0 , batchedParticipationOnlineApplications.size());


Any help is appreciated.

1 Answer 1


You must call Test.stopTest() following Database.executeBatch() and prior to running any queries and assertions that validate the action of the batch.

From Using Batch Apex:

Use the Test methods startTest and stopTest around the executeBatch method to ensure that it finishes before continuing your test. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously. If you don’t include the executeBatch method within the startTest and stopTest methods, the batch job executes at the end of your test method.

In your current test method, your batch class's start(), execute(), and finish() methods will not execute until Test.stopTest() is called at the very end of the unit test.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .