You need to sign in to do that
Don't have an account?
JohnDurai
Test class to cover execute method last modified date cant writeable
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 in batch apex so scope does not returning record. can some one help how to cover this?
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;
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);
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;
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);
Test.stopTest();
}
}
Your batch class execute method, you need to pass object name instead of sObject. Refer the below link to set the lastmodified date.
https://salesforce.stackexchange.com/questions/62/unit-testing-code-which-has-logic-around-the-createddate
If this helps, Please mark it as best answer.
Thanks!!