You need to sign in to do that
Don't have an account?
JohnDurai
lastmodified date as past in test class
Hi All - I am trying to cover the test class for batch apex, but i could not able to cover execute method since my test record does not match the query condition (lastmodifiedDate) in batch apex so scope does not returning record. can some one help how to cover the execute method here. Thanks!
Here is my Test Class:
@isTest
public class TestpurgeCampaignRecords {
static testMethod void TestpurgeCampaignRecordsMethod(){
//List<CampaignMember> campaignMembers = new List<CampaignMember>();
Contact c = new Contact(
FirstName = 'User',
LastName = 'Test ' );
insert c;
Id cMRecordId = Schema.SObjectType.CampaignMember.getRecordTypeInfosByName().get('Hypercare Customer').getRecordTypeId();
Campaign campaign = new Campaign(Name = 'Test Campaign ',IsActive = true, CampaignMemberRecordTypeId=cMRecordId);
insert campaign;
string recordTypeId =system.Label.CampaignMemberRecordtype;
system.debug('recordTypeId' +recordTypeId);
integer dayCounts = Integer.valueof(system.label.LastmodifiedDateforCM);
system.debug('dayCounts' +dayCounts);
list<CampaignMember> cmList = new List<CampaignMember>();
CampaignMember campaignMember = new CampaignMember();
campaignMember.ContactId = c.Id;
//campaignMember.RecordTypeId = cMRecordId;
campaignMember.CampaignId = campaign.Id;
campaignMember.LeadId=null;
campaignMember.Status = 'Open';
campaignMember.Locked_By__c = null;
campaignMember.Root_Cause__c ='Address discrepancy';
campaignMember.Resolution_Status__c ='Resolved';
//campaignMember.LastModifiedDate = System.today() - 1;
//insert campaignMember;
cmList.add(campaignMember);
insert cmList;
//insert campaignMembers;
Test.startTest();
String schTime = '0 0 12 * * ?';
purgeCampaignRecords pcr = new purgeCampaignRecords();
system.schedule('TestUpdateConAccJob', schTime, pcr);
//system.AssertException;
database.executeBatch(pcr);
//System.assertEquals(1, [SELECT COUNT() FROM CampaignMember WHERE Id = :cmList[0].id]);
Test.stopTest();
}
Here is my Test Class:
@isTest
public class TestpurgeCampaignRecords {
static testMethod void TestpurgeCampaignRecordsMethod(){
//List<CampaignMember> campaignMembers = new List<CampaignMember>();
Contact c = new Contact(
FirstName = 'User',
LastName = 'Test ' );
insert c;
Id cMRecordId = Schema.SObjectType.CampaignMember.getRecordTypeInfosByName().get('Hypercare Customer').getRecordTypeId();
Campaign campaign = new Campaign(Name = 'Test Campaign ',IsActive = true, CampaignMemberRecordTypeId=cMRecordId);
insert campaign;
string recordTypeId =system.Label.CampaignMemberRecordtype;
system.debug('recordTypeId' +recordTypeId);
integer dayCounts = Integer.valueof(system.label.LastmodifiedDateforCM);
system.debug('dayCounts' +dayCounts);
list<CampaignMember> cmList = new List<CampaignMember>();
CampaignMember campaignMember = new CampaignMember();
campaignMember.ContactId = c.Id;
//campaignMember.RecordTypeId = cMRecordId;
campaignMember.CampaignId = campaign.Id;
campaignMember.LeadId=null;
campaignMember.Status = 'Open';
campaignMember.Locked_By__c = null;
campaignMember.Root_Cause__c ='Address discrepancy';
campaignMember.Resolution_Status__c ='Resolved';
//campaignMember.LastModifiedDate = System.today() - 1;
//insert campaignMember;
cmList.add(campaignMember);
insert cmList;
//insert campaignMembers;
Test.startTest();
String schTime = '0 0 12 * * ?';
purgeCampaignRecords pcr = new purgeCampaignRecords();
system.schedule('TestUpdateConAccJob', schTime, pcr);
//system.AssertException;
database.executeBatch(pcr);
//System.assertEquals(1, [SELECT COUNT() FROM CampaignMember WHERE Id = :cmList[0].id]);
Test.stopTest();
}
Can you share the apex class. i will check and get back to you.
Thanks!!
Refer the below link have solution to modify lastmodified date in the test class.
https://salesforce.stackexchange.com/questions/147056/how-to-change-the-lastmodifieddate-of-a-record-in-a-test-class
If this helps, Please mark it as best answer.
Thanks!!
global class purgeCampaignRecords implements Database.Batchable <sObject>,Schedulable,Database.Stateful {
global Database.QueryLocator start(Database.BatchableContext bc ) {
HypercareBatchDelete__mdt[] dayscount = [SELECT Id, RecirdTypeId__c, DaysCount__c FROM HypercareBatchDelete__mdt];
system.debug('dayscount' +dayscount[0].DaysCount__c);
integer noOfDays = Integer.valueof(dayscount[0].DaysCount__c);
string recordTypeId = dayscount[0].RecirdTypeId__c;
system.debug('dayscount' +dayscount[0].DaysCount__c);
if(Test.isRunningTest()){
return Database.getQueryLocator([SELECT Id, Status,RecordTypeId, Resolution_Status__c, LastModifiedDate FROM CampaignMember where RecordTypeId ='0120p000000Fy09AAC' AND LastModifiedDate <= LAST_N_DAYS:0]);
}
else{
String Query = 'SELECT Id, Status,RecordTypeId, Resolution_Status__c, LastModifiedDate FROM CampaignMember where '+
'RecordTypeId=\''+recordTypeId+'\' AND (Resolution_Status__c =\'Resolved\'' + 'OR Resolution_Status__c =\'Closed\'' +
')AND LastModifiedDate < LAST_N_DAYS:'+noOfDays;
return Database.getQueryLocator(Query);
}
}
global void execute(Database.BatchableContext BC , List<CampaignMember> campaignMemberScope) {
system.debug('CM Records' + campaignMemberScope);
if(campaignMemberScope !=NULL && campaignMemberScope.size()>0){
try{
Database.DeleteResult[] result = Database.delete(campaignMemberScope, false);
} catch(Exception e){
system.debug('Exception Caught:' +e.getmessage());
}
}
}
global void finish(Database.BatchableContext BC){
//need to check if any email confirmation
}
//to schedule this class
global void execute(SchedulableContext sc){
Database.executeBatch(new purgeCampaignRecords());
}
}