You need to sign in to do that
Don't have an account?
Zane Prater 15
Test class is not covering the execute Database.batchable where I am deleting the list returned from the query locator
global with sharing class JunkQueueBatchJob implements Database.Batchable<sObject> { global Database.QueryLocator start(Database.BatchableContext BC) { String query = 'select id from case where owner.Name = \'To Be Deleted\' AND createddate < LAST_N_MONTHS:5 ORDER BY Createddate DESC '; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<Case> scope) { delete scope; } global void finish(Database.BatchableContext BC) { } } //excerpt from test method list<Case> mktcses = new list<Case>(); Id mktcse = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Marketing Component').getRecordTypeId(); Case cse1 = new Case(); cse1.RecordTypeId = mktcse; cse1.AccountId = acctObj.Id; cse1.ContactId = conObj.Id; cse1.Status = 'In Progress'; cse1.Description = 'Test'; cse1.Subject = 'Dental Equipment Broadcast'; cse1.Materials_Due_Date__c = date.today().addDays(60); mktcses.add(cse1); Case cse2 = new Case(); cse2.RecordTypeId = mktcse; cse2.AccountId = acctObj.Id; cse2.ContactId = conObj.Id; cse2.Status = 'In Progress'; cse2.Description = 'Test'; cse2.Subject = 'Dental Webinar'; cse2.Materials_Due_Date__c = date.today().addDays(60); cse2.CreatedDate = date.today().addMonths(-6); system.debug('createddate*****'+cse2.CreatedDate); mktcses.add(cse2); insert mktcses; test.startTest(); id sysid; For(Group grp:[Select id, name, owner.name from Group where type ='Queue' AND Name = 'To Be Deleted']) { sysid = grp.id; } cse2.OwnerId = sysid; update cse2; system.debug('cse2 owner name_____'+sysid); //JunkQueueBatchJob is chained with batch class called from scheduler Den_Marketing_Scheduler dms = new Den_Marketing_Scheduler(); String sch = '0 0 23 * * ?'; system.schedule('denmailer', sch, dms); system.debug('Cases after Deletion*****'+mktcses.size()); //should return with 1 but is returning 2 (cse2 should have been deleted) test.stopTest(); } //system.assertEquals(1, mktcses.size());
Hi Zane,
In the test class, while creating the sample data for case record, you cannot write the value for created date. It is system field read only.
Field is not writeable: cse1.CreatedDate
Regards
I see you also posted on https://salesforce.stackexchange.com/questions/339126/why-test-class-not-covering-the-execute-database-batchable-where-i-am-deleting-t
Does the solution provided work for you- All async methods (batch, queueable. batchable, future) are executed at the Test.stopTest() moment in unit tests. Therefore you can move your asserts after test.stopTest to check it.
See related: https://salesforce.stackexchange.com/questions/244791/how-do-i-test-asynchronous-apex section "Nested Asynchronous Code"
If this information helps, please mark the answer as best. Thank you
System.AssertException: Assertion Failed: Expected: 1, Actual: 2
Not executing this line:
global void execute(Database.BatchableContext BC, List<Case> scope)
{
delete scope;