3

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

2

From my reading of the documentation and various blogs, this line:

List<sObject> ls = Test.loadData(Lead.sObjectType, 'leadFile1');

should insert the data into the database. And as long as the static resource CSV includes a CreatedDate column, that date will be used. So your batch job and your test should be able to read that data from the database and it should be possible to get your test working.

I suggest you check that this loading process is actually working by doing something like:

System.debug('leads=' + [select Name, Lead_Round_Robin_Assignment__c, OwnerId,
        CreatedToday__c, Created15OrMore__cCreatedDate from Lead]);

immediately after the load. (And you could do the same again after your batch has run.)

I presume you have checked that the CreatedToday__c and Created15OrMore__c are based on the CreatedDate and not some other field?

1

I assume that Created15OrMore__c is a formula field that determines if the record is more than 15 minutes old based on the CreatedDate? You say you believe the issue you are having is that as the records have just been created at the point you run the test, none are returning as more than 15 minutes old?

If so, you could get this test to pass by simply removing Created15OrMore__c = true from the filter criteria for the query in the test class. This is not necessarily ideal but it is potentially a solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.