1

i have a batch class which i am scheduling for run. and i have a test class for the batch class.

the batch class is working fine . but the code coverage is 44% .

important note: when the batch class is run and if it processes any data then the code coverage is going to 100% and if it don't process any then its dropping to 44%

My Batch Class :

global class batchleadUpdate implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Name,Status ,OwnerId  FROM Lead WHERE Status != \'Closed - Converted\' AND CreatedDate < LAST_N_DAYS:30';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Lead> scope)
    {
        User u = [select Id from User where Profile.Name='System Administrator' Limit 1];

         for(Lead le : scope)
         {
             le.OwnerId = u.Id  ;            
         }
         update scope;
    }   
    global void finish(Database.BatchableContext BC)
    {
    }
}

Test Class :

@isTest(SeeAllData=true)
  private class batchleadUpdate_Test { 

static testMethod void batchleadUpdate_TestMethod (){
     Profile prof = [select id from profile where name='system Administrator'];
     User usr = new User(alias = 'usr', email='us.name@vmail.com',
                emailencodingkey='UTF-8', lastname='lstname',
                timezonesidkey='America/Los_Angeles',
                languagelocalekey='en_US',
                localesidkey='en_US', profileid = prof.Id,
                username='testuser128@testorg.com');
                insert usr;

          Lead lead=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Open');
insert lead;  
Lead ledRec1 = [SELECT Id,Name,Status,OwnerId FROM Lead WHERE Status != 'Closed - Converted' limit 1 ];

   ledRec1.OwnerId = usr.Id;
   update ledRec1 ;
   Test.StartTest();

   batchleadUpdate objBatch = new batchleadUpdate();
   ID batchprocessid = Database.executeBatch(objBatch);
   Test.StopTest();
  }
}
  • 1
    Are you incorporating test data into your Apex tests? If you look at the documentation, it covers different methods for generating test data. I've just started exploring SmartFactory, too. – Zayne at Loft Oct 1 '14 at 20:29
  • Is your query returning anything? – EricSSH Oct 1 '14 at 22:16
  • NO becuase i dont have any date meeting this condition its a new org so i don't have a past data – user9982 Oct 1 '14 at 22:17
  • if i can insert a lead which will have a created date meeting this condition then the update will cover but i don't know how to do this . – user9982 Oct 1 '14 at 22:19
  • When your inserting the Lead can you just put CreatedDate = system.today() .. Zayne was right, you have to treat your test class like your creating a record manually – EricSSH Oct 1 '14 at 22:22
2

In order to generate testdata where the createdDate < today, you have to use JSON.deserialize (similarly, if you want to generate test leads with a convertedDate other than today.

This solution is outlined here for convertedDates and below for createdDate. Props to @sfdcfox for the original solution.

Lead [] lList = (Lead[]) JSON.deserialize(
    '[{"lastname" : "foo", "company" : "bar", "CreatedDate" : "2005-01-01"}]',List<Lead>.class);
insert lList;   
| improve this answer | |
  • Hey Crop, thanks a tone its worked and the test class is now 100% . amazing – user9982 Oct 2 '14 at 1:30
  • Great answer. You should create a new question titled something like How do I set CreatedDate in unit tests? and add that as the answer so that it is easier for other people to find. I assume the same holds true for LastModifiedDate – BarCotter Oct 2 '14 at 9:39
  • there was a long discussion about this here: salesforce.stackexchange.com/questions/62/… . I was kind of surprised myself that the DML would work as that discussion said it wouldn't – cropredy Oct 2 '14 at 22:27

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.