I have a batch class that is used to clear out all the records of an object. I'm trying to unit test the case where one of the records fails to be deleted. I'm trying to have a record fail by locking it in my unit test.
public static testMethod void testDeleteFailure() {
//Test data is setup and I have various assertions here to make sure my test data is good
Test.StartTest();
List<My_Object__c> lockedItems = [SELECT Id FROM My_Object__c LIMIT 2 FOR UPDATE];
BatchDeleteObject deleteJob = new BatchDeleteObject('My_Object__c');
Database.executeBatch(deleteJob);
Test.StopTest();
Integer newCount = [SELECT COUNT() FROM My_Object__c];
System.assertEquals(lockedItems.count(), newCount, 'There should be ' + lockedItems.count() + ' still in the database.');
}
However, my records aren't locking so they are being deleted just fine. Is there another way to lock these records? Is there a better way to test to make sure any failures are handled appropriately?
Test.startTest
.Test.{start, stop}Test()
(it's described as resetting limits only), particularly when async jobs are submitted. Does the batch class execute in the same transaction as theFOR UPDATE
?