function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VSK98VSK98 

Test class for sample batch class

Hi All,

I am writing test class for batch class.........but it covered upto 50 % only ..........Here is batch class 
global class batchAccountUpdateSLA implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
     date d = system.today();   
String query = 'SELECT Id,Name FROM Account where after1day__c = :d
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Account> scope) {
         for(Account a : scope)
         {
             a.SLA__c= 'Gold';            
         }
         update scope;
    }   
    
    global void finish(Database.BatchableContext BC) {
    }
}

Here is my test class

 
@isTest
 public class batchAccountUpdateSLA_Test{
 Static testmethod void batchAccountUpdateSLA_TestMethod(){
 
 Date d = system.today().addDays(3);
     Account acc = new Account();
     acc.name = 'Test';
     acc.Fax = '12345';
     insert acc;
      Test.StartTest(); 
      batchAccountUpdateSLA bacth = new batchAccountUpdateSLA ();
      ID batchprocessid = database.Executebatch(bacth ); 
      Test.StopTest();
    }
  }
Formula field is After1day code:
DATEVALUE(CreatedDate) + 
CASE( 
MOD(DATEVALUE(CreatedDate)- DATE(1900, 1, 7), 7), 
0, 2, 
1, 1, 
2, 1, 
3, 1, 
4, 1, 
5, 3, 
6, 3, 
0)



Adv thnx
Siv
 
Best Answer chosen by VSK98
Marek Kosar_Marek Kosar_
That's because your logic is based on system `CreatedDate` field - so in `start` part of batch class you don't query anything, `execute` is not covered.
From Spring '16, new method .setCreatedDate is available for testing:
Account acc = new Account();
acc.name = 'Test';
acc.Fax = '12345';
insert acc;

Datetime now = Datetime.now();
Test.setCreatedDate(acc.Id, now); //or whatever date you want here to test it properly

Marek

All Answers

Marek Kosar_Marek Kosar_
That's because your logic is based on system `CreatedDate` field - so in `start` part of batch class you don't query anything, `execute` is not covered.
From Spring '16, new method .setCreatedDate is available for testing:
Account acc = new Account();
acc.name = 'Test';
acc.Fax = '12345';
insert acc;

Datetime now = Datetime.now();
Test.setCreatedDate(acc.Id, now); //or whatever date you want here to test it properly

Marek
This was selected as the best answer
VSK98VSK98
Getting error 
Method does not exist or incorrect signature: Test.setCreatedDate(acc.Id, Datetime)
Marek Kosar_Marek Kosar_
Probably you still have winter'15 release in your org, this feature is(will be) in spring'16.. 
there is nice @bobbuzzard blog about it -> http://bobbuzzard.blogspot.co.uk/2016/01/salesforce-spring-16-createddate-and.html
VSK98VSK98
User-added image
My box is Spring '16 ..................
Marek Kosar_Marek Kosar_
could you please copy-paste your test class once again(actual version)?
I tried it in my spring16 sandbox and it works fine