0

I am trying to increase test class coverage of batch class..how to fulfill if condition in batch class.I am using this batch class for sending email 15 days prior to release date.I am stuck at test class.Can some help me..

Test Class

public class test_batch_reduced_send_notifications {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test

         //Database.BatchableContext BC;
          List<ID> acRecs=new List<ID>();
          Account acc=new Account();
          acc.LastName='Test Account';
          acc.Student_Status__pc='New';
          acc.Phone='999999999';
          acc.Release_Date__c=Date.today().addDays(14);
          insert acc;
          acRecs.add(acc.id);
        Test.startTest();
        Batch_reduced_send_notification brsn=new Batch_reduced_send_notification();
          /*Database.BatchableContext BC;
          brsn.start(BC);*/

        Date tDay=Date.today();
        brsn.query='Select id,FirstName,\Release_Date__c,PersonContactId,personemail from account where Release_Date__c!=null and Release_Date__c>=:tDay';
        brsn.tDay=tDay;
        Database.executeBatch(brsn);
        Test.stopTest();
    }
}

Batch Class

  global with sharing class Batch_reduced_send_notification implements Database.Stateful,Database.Batchable<sObject>{

        global String query; 
        global Date tDay;

        List<String> accIds;
        List<String> emailRec;
        List<String> stuEmail;

        global Database.QueryLocator start(Database.BatchableContext BC)
        {
          return Database.getQueryLocator(query);  
        }

        global void execute(Database.BatchableContext BC,  List<Account> scope)  
        { 
           accIds=new List<String>();
           emailRec=new List<String>();
           stuEmail=new List<String>();
           **If(scope[0].Release_Date__c.addDays(-14)==Date.today())
           {**
              for(Account acc:scope)
              {
                accIds.add(acc.PersonContactId);
                if(acc.personemail!=null)
                stuEmail.add(acc.personemail);
              }
}
1

I think you have a typo in your test SoQL query...

        brsn.query='Select id,FirstName,\Release_Date__c,PersonContactId,personemail from account where Release_Date__c!=null and Release_Date__c>=:tDay';

shouldn't it be Release_Date__c and not \Release_Date__c ?

    brsn.query='Select id,FirstName,Release_Date__c,PersonContactId,personemail from account where Release_Date__c!=null and Release_Date__c>=:tDay';

In any case if you don't want to bother with date calculations you can use the Test.isRunningTest() in your if clause to "force" the execution of this part of your code.

I don't know the business logic details, so I am not sure whether scope[0] is ok. Let's assume it is :)

Your if statement can be rewritten as follows

If(scope[0].Release_Date__c.addDays(-14)==Date.today() || Test.isRunningTest())
{
...
}

for more details see http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_Test_static_methods.htm and http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests

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.