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 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
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
All Answers
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
Thank you very much.
Its working.