+ Start a Discussion
SFDC Coder 8SFDC Coder 8 

Need help to Increase code coverage for Batch Apex Insert

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

 
Best Answer chosen by SFDC Coder 8
Amit Chaudhary 8Amit Chaudhary 8
Please 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) {
	} 
}

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please update you 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) {
	} 
}
And try below test class
@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();
	}
	
}
Let us know if this will help you

 
SFDC Coder 8SFDC Coder 8

Hi Amit,

Thank you for your answer. Test class is working fine.
But, It is not working. That is batch class is not working properly
dayOfWeek value is getting "Null" value.
So, its not entering into SOQL query which I require on daily basis. It is entering into last SOQL query in my batch job.
Please suggest me what change I need to do.

Thank you

Amit Chaudhary 8Amit Chaudhary 8
Please 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) {
	} 
}

 
This was selected as the best answer
SFDC Coder 8SFDC Coder 8
Hi Amit,
Thank you very much.
Its working.