I'm working on a batch job that will change a lead record's Owner when the record was created over 15 minutes ago and is still owned by a particular user.
Here's the batch job. This works in practice as expected. The test class however is not working.
I think the reason the test class is not working is because I need to use a static resource to load my test data to test this...because of the need to set created date (a normally uneditable system field) to 15 minutes ago. This approach of using static resources to insert test data appears not to insert the test record or records into the database. Therefore my final system assertequals is returning a value of "0".
Here's the batch job:
global class LeadOwnerReassignmentBatch implements Database.Batchable<sObject>{
Global String query;
Global Id fromUserId;
global Database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
List<Lead> leads = new List<Lead>();
for(sObject s : scope){
Lead l = (Lead) s;
l.OwnerId = l.Lead_Round_Robin_Assignment__c;
leads.add(l);
}
update leads;
}
global void finish(Database.BatchableContext BC){
}
}
Here's the test class:
@isTest
public class LeadOwnerReassignmentBatchTest {
public static testMethod void LeadOwnerReassignmentBatchTest() {
user u = [SELECT ID, Alias FROM User
WHERE Alias='Marketo'];
List<User> userList = [SELECT ID, Alias FROM User
WHERE Alias!='Marketo'];
List<sObject> ls = Test.loadData(Lead.sObjectType, 'leadFile1');
System.AssertEquals(
database.countquery('SELECT COUNT()'
+' FROM Lead WHERE OwnerId=\'' + u.Id + '\''),
1);
Test.StartTest();
LeadOwnerReassignmentBatch reassign = new LeadOwnerReassignmentBatch();
reassign.query='SELECT ID, Name, Lead_Round_Robin_Assignment__c, CreatedToday__c, Ownerid ' +
'FROM Lead ' +
'WHERE CreatedToday__c = true AND Created15OrMore__c = true AND OwnerId=\'' + u.Id + '\'' +
' LIMIT 200';
reassign.fromUserId = u.Id;
ID batchprocessid = Database.executeBatch(reassign);
Test.StopTest();
System.AssertEquals(
database.countquery('SELECT COUNT()'
+' FROM Lead WHERE OwnerId IN: userList AND Id IN: ls'),
1);
}
}
All that is happening here is the class changes owner when the batch is run. So all my test class is doing is inserting a record, updating owner view a run of the batch, and then confirming owner changed. But that final system assertequals is not working I think b/c I'm not committing data to the database due to my approach of using the static resource.
Does anyone see anything in the code that I could change or has anyone run into this before? I'm not seeing any useful solutions on the forum.
Thanks guys