You need to sign in to do that
Don't have an account?
SFDC 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
Test class
Please help me how to cover these lines.
Thanks in Advance
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
One way perhaps is to change your code and pass the Date as parameter...
then you can do test like this:
Best Regards...
I made changes as following to my Batch Apex
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
1 ) https://developer.salesforce.com/forums/ForumsMain?id=9060G000000XhjP
Update your Batch job like below Test class like below
this line of code is confusing:
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.
Good Luck!