-2

I have a batch job that delete records from object that were created more than 13 month ago. Please help me to write unit test for this batch. Thanks

@IsTest private class ABI_CXC_DeleteNVMData_Batch_Test {

public String query;

@TestSetup
static void setup()
{
    List<NVMContactWorld__InteractionEvent__c> testEvent = new List<NVMContactWorld__InteractionEvent__c>();
    testEvent.add(new NVMContactWorld__InteractionEvent__c(Name = 'Test Event'));
    insert testEvent;
    Datetime createDate = Datetime.now().addMonths(-13);
    Test.setCreatedDate(testEvent[0].Id, createDate);
}

@IsTest static testMethod void positiveTestMethod() {
    Test.startTest();
    ABI_CXC_DeleteNVMData_Batch records = new ABI_CXC_DeleteNVMData_Batch(query);
    Database.executeBatch(records);
    Test.stopTest();
    System.assertEquals(1, [SELECT Id FROM NVMContactWorld__InteractionEvent__c WHERE Name = 'Test Event']);
}

}

0
1

If you are new to writing Apex tests start by working through these Apex Testing Trailhead units.

Asynchronous logic is run when you call Test.stopTest(). So to test the batch job, in the unit test method(s):

  • Insert some data suitable for the batch job
  • Call Test.startTest
  • Database.execute your batch job
  • Call Test.stopTest
  • Assert that the results are as expected
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.