+ Start a Discussion
SFDC Coder 8SFDC Coder 8 

Need help to Increase code coverage for Batch Apex

Hi All,
I have written a batch apex to insert records.
I have written a test class for the same. But some lines are not covering.
Please help me cover those lines.
Here is my code
global class batchInsert Implements Schedulable, Database.Batchable<sObject>{
global void execute(SchedulableContext sc) {
    Database.executeBatch(this);
}

global database.queryLocator start(Database.BatchableContext BC) {
    
    Date d = System.today(); 
    Datetime dt = (DateTime)d; 
    String dayOfWeek = dt.format('EEEE');
   
    if (dayOfWeek == 'Monday')
        return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Monday__c='Yes']);
    else if (dayOfWeek == 'Tuesday')
        return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Tuesday__c='Yes']);
    else if (dayOfWeek == 'Wednesday')
        return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Wednesday__c='Yes']);
    else if (dayOfWeek == 'Thursday')
        return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Thursday__c='Yes']);
    else if (dayOfWeek == 'Friday')
        return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Friday__c='Yes']);
    else if (dayOfWeek == 'Saturday')
        return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Saturday__c='Yes']);
    else 
        return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Sunday__c='Yes']);
    

} 

global void execute(Database.BatchableContext BC, list <Obj1__c> scope) {

    List <Obj2__c> creList = new List<Obj2__c>();
    Date d = System.today(); 
    Datetime dt = (DateTime)d; 
    String dayIs = dt.format('EEEE');
 
    
    for(Obj1__c c : scope) {
            Obj2__c obj             = new Obj2__c();
        
           
            
            obj.F1__c=c.F1__c;
            obj.F2__c=c.F2__c;
           
            if(dayIs=='Monday' && c.Monday__c == 'Yes'){
            obj.F3__c='Yes';
            }
            else if(dayIs=='Monday' &&c.Monday__c == 'No'){
            obj.F3__c='No';
            }
            else if(dayIs=='Tuesday' &&c.Tuesday__c == 'Yes'){
            obj.F3__c='Yes';
            }
            else if(dayIs=='Tuesday' &&c.Tuesday__c == 'No'){
            obj.F3__c='No';
            }
            else if(dayIs=='Wednesday' &&c.Wednesday__c == 'Yes'){
            obj.F3__c='Yes';
            }
            else if(dayIs=='Wednesday' &&c.Wednesday__c == 'No'){
            obj.F3__c='No';
            }
            else if(dayIs=='Thursday' &&c.Thursday__c == 'Yes'){
            obj.F3__c='Yes';
            }
            else if(dayIs=='Thursday' &&c.Thursday__c == 'No'){
            obj.F3__c='No';
            }
            else if(dayIs=='Friday' &&c.Friday__c == 'Yes'){
            obj.F3__c='Yes';
            }
            else if(dayIs=='Friday' &&c.Friday__c == 'No'){
            obj.F3__c='No';
            }
            else if(dayIs=='Saturday' &&c.Saturday__c == 'Yes'){
            obj.F3__c='Yes';
            }
            else if(dayIs=='Saturday' &&c.Saturday__c == 'No'){
            obj.F3__c='No';
            }
            else if(dayIs=='Sunday' &&c.Sunday__c == 'Yes'){
            obj.F3__c='Yes';
            }
            else if(dayIs=='Sunday' &&c.Sunday__c == 'No'){
            obj.F3__c='No';
            }
           
           creList.add(obj);
    } 

    
        insert creList;
     
} 

global void finish(Database.BatchableContext BC) {


} 
}

Test class
@isTest(seealldata=true)
public class  testSendeBatch
{

Public static testMethod void method3()
{ 

Obj1__c cp=new Obj1__c();
cp.F1__c='Test';
cp.F2__c='Test2';

batchInsert sb=new batchInsert();
     
        Test.startTest();
        Database.executeBatch(sb);
        Test.stopTest();
       
    }
 
 
  }
I am not able to cover lines between Line number 12 to 25 and Line number 46 to 87.
Please help me how to cover these lines.
Thanks in Advance
 
FearNoneFearNone
SFDC Coder 8,

One way perhaps is to change your code and pass the Date as parameter...
global class batchInsert Implements Schedulable, Database.Batchable<sObject>{

	private Date d;
	
	public batchInsert(Date _d) {
        d = (DateTime)_d;
    }
    
    
	global database.queryLocator start(Database.BatchableContext BC) {
	    Datetime dt = d; 
	    String dayOfWeek = dt.format('EEEE');
	    ...
	} 

	global void execute(Database.BatchableContext BC, list <Obj1__c> scope) {
	    List <Obj2__c> creList = new List<Obj2__c>();
	    Datetime dt = (DateTime)d; 
	    String dayIs = dt.format('EEEE');
	    ...
	}

then you can do test like this:
Test.startTest();
batchInsert b;
b = new batchInsert(Date.newInstance(2016, 12, 1);); //thu
database.executeBatch(b);
b = new batchInsert(Date.newInstance(2016, 12, 2);); //fri
database.executeBatch(b);
b = new batchInsert(Date.newInstance(2016, 12, 3);); //sat
database.executeBatch(b);
b = new batchInsert(Date.newInstance(2016, 12, 4);); //sun
database.executeBatch(b);
b = new batchInsert(Date.newInstance(2016, 12, 5);); //mon
database.executeBatch(b);
b = new batchInsert(Date.newInstance(2016, 12, 6);); //tue
database.executeBatch(b);
b = new batchInsert(Date.newInstance(2016, 12, 7);); //wed
database.executeBatch(b);
Test.stopTest();

Best Regards...
 
SFDC Coder 8SFDC Coder 8
FearNone,
I made changes as following to my Batch Apex
global class batchInsert Implements Schedulable, Database.Batchable<sObject>{

	private Date d;
	
	public batchInsert(Date d) {
        d = (Date)d;
    }
    
    
	global database.queryLocator start(Database.BatchableContext BC) {
	    Datetime dt = d; 
	    String dayOfWeek = dt.format('EEEE');
	    ...
	} 

	global void execute(Database.BatchableContext BC, list <Obj1__c> scope) {
	    List <Obj2__c> creList = new List<Obj2__c>();
	    Datetime dt = (DateTime)d; 
	    String dayIs = dt.format('EEEE');
	    ...
	}

I am getting null value at at 'String dayOfWeek = dt.format('EEEE')'
I am not getting any values into dayOfWeek.
Please what change I need to do for this
SFDC Coder 8SFDC Coder 8
I am getting error as "System.NullPointerException: Attempt to de-reference a null object " for dayOfWeek while running test class
Amit Chaudhary 8Amit Chaudhary 8
Look like this issue is resolved in below post
1 ) https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XhjP

Update your Batch job like below
global class batchInsert Implements  Database.Batchable<sObject>
{

	String strDay;
	global database.queryLocator start(Database.BatchableContext BC) 
	{
    
		Date d = System.today(); 
		Datetime dt = (DateTime)d; 

		String dayOfWeek ;
		if(strDay != null && strDay!= '')
		{
			dayOfWeek = strDay;
		}
		else{
			dayOfWeek = dt.format('EEEE');
		}
		
		if (dayOfWeek == 'Monday')
			return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Monday__c='Yes']);
		else if (dayOfWeek == 'Tuesday')
			return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Tuesday__c='Yes']);
		else if (dayOfWeek == 'Wednesday')
			return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Wednesday__c='Yes']);
		else if (dayOfWeek == 'Thursday')
			return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Thursday__c='Yes']);
		else if (dayOfWeek == 'Friday')
			return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Friday__c='Yes']);
		else if (dayOfWeek == 'Saturday')
			return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Saturday__c='Yes']);
		else 
			return database.getQueryLocator([SELECT Id,F1__c,F2__c from Obj1__c where Sunday__c='Yes']);
	}

	global void execute(Database.BatchableContext BC, list <Obj1__c> scope) 
	{
		List <Obj2__c> creList = new List<Obj2__c>();
    
		for(Obj1__c c : scope) 
		{
            Obj2__c obj = new Obj2__c();
            obj.F1__c=c.F1__c;
            obj.F2__c=c.F2__c;
           
            if(  c.Monday__c == 'Yes'  && c.Tuesday__c == 'Yes' && c.Wednesday__c == 'Yes' && c.Thursday__c == 'Yes'  && c.Friday__c == 'Yes' && c.Sunday__c == 'Yes' && c.Saturday__c == 'Yes') )
			{
				obj.F3__c='Yes';
            }
            else 
				obj.F3__c='No';
            }
           creList.add(obj);
		}
        insert creList;
	} 

	global void finish(Database.BatchableContext BC) {
	} 
}
Test class like below
@isTest
public class  testSendeBatch
{

	Public static testMethod void method1()
	{ 

		Obj1__c cp=new Obj1__c();
		cp.F1__c='Test';
		cp.F2__c='Test2';
		cp.Monday__c='Yes';
		cp.Tuesday__c='Yes';
		cp.Wednesday__c='Yes';
		cp.Thursday__c='Yes';
		cp.Friday__c='Yes';
		cp.Saturday__c='Yes';
		cp.Sunday__c='Yes';
		insert cp;
		

		Test.startTest();
			batchInsert sb=new batchInsert();
			sb.strDay ='Monday';
			Database.executeBatch(sb);
		Test.stopTest();
	}
	Public static testMethod void method2()
	{ 

		Obj1__c cp=new Obj1__c();
		cp.F1__c='Test';
		cp.F2__c='Test2';
		cp.Monday__c='Yes';
		cp.Tuesday__c='Yes';
		cp.Wednesday__c='Yes';
		cp.Thursday__c='Yes';
		cp.Friday__c='Yes';
		cp.Saturday__c='Yes';
		cp.Sunday__c='Yes';
		insert cp;
		

		Test.startTest();
			batchInsert sb=new batchInsert();
			sb.strDay ='Tuesday';
			Database.executeBatch(sb);
		Test.stopTest();
	}
	Public static testMethod void method3()
	{ 

		Obj1__c cp=new Obj1__c();
		cp.F1__c='Test';
		cp.F2__c='Test2';
		cp.Monday__c='Yes';
		cp.Tuesday__c='Yes';
		cp.Wednesday__c='Yes';
		cp.Thursday__c='Yes';
		cp.Friday__c='Yes';
		cp.Saturday__c='Yes';
		cp.Sunday__c='Yes';
		insert cp;
		

		Test.startTest();
			batchInsert sb=new batchInsert();
			sb.strDay ='Wednesday';
			Database.executeBatch(sb);
		Test.stopTest();
	}
	
	Public static testMethod void method4()
	{ 

		Obj1__c cp=new Obj1__c();
		cp.F1__c='Test';
		cp.F2__c='Test2';
		cp.Monday__c='Yes';
		cp.Tuesday__c='Yes';
		cp.Wednesday__c='Yes';
		cp.Thursday__c='Yes';
		cp.Friday__c='Yes';
		cp.Saturday__c='Yes';
		cp.Sunday__c='Yes';
		insert cp;
		

		Test.startTest();
			batchInsert sb=new batchInsert();
			sb.strDay ='Thursday';
			Database.executeBatch(sb);
		Test.stopTest();
	}
	
	Public static testMethod void method5()
	{ 

		Obj1__c cp=new Obj1__c();
		cp.F1__c='Test';
		cp.F2__c='Test2';
		cp.Monday__c='Yes';
		cp.Tuesday__c='Yes';
		cp.Wednesday__c='Yes';
		cp.Thursday__c='Yes';
		cp.Friday__c='Yes';
		cp.Saturday__c='Yes';
		cp.Sunday__c='Yes';
		insert cp;
		

		Test.startTest();
			batchInsert sb=new batchInsert();
			sb.strDay ='Friday';
			Database.executeBatch(sb);
		Test.stopTest();
	}
	
}


 
FearNoneFearNone
SFDC Coder 8,

this line of code is confusing:
d = (Date)d
global class batchInsert Implements Schedulable, Database.Batchable<sObject>{
	private Date d;

	public batchInsert(Date d) {
	   d = (Date)d;
	}
this means that the local "d" is assigned with the local "d".
therefore global "d" will always be null.

FIX:
just rename the local "d" and you are good to go. and you can also remove the casting.
global class batchInsert Implements Schedulable, Database.Batchable<sObject>{
	private Date d;

    public batchInsert(Date paramDate) {
       d = paramDate;
    }


Good Luck!