You need to sign in to do that
Don't have an account?
Rupeshk67
Need help on batch apex test class
i have a batch class as below :
-------------------------------------------------------------
public class DM_TechAvailability implements Database.Batchable<sObject> {
public DM_TechAvailability(){
}
public Database.QueryLocator start (Database.BatchableContext BC){
string BLANK = '';
string nulll = NULL;
String query = 'select id, SV_Date_String__c, SV_Date__c from SV_Technician_Availability__c
where SV_Date__c != null and (SV_Date_String__c =: BLANK or SV_Date_String__c =: nulll)';
system.debug('1*'+Database.getQueryLocator(query));
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List<sObject> xTcope){
List<SV_Technician_Availability__c> taList = new List<SV_Technician_Availability__c>();
for(sObject eachTcope: xTcope){
SV_Technician_Availability__c eachTA = (SV_Technician_Availability__c)eachTcope;
eachTA.SV_Date_String__c = String.ValueOf(eachTA.SV_Date__c);
taList.add(eachTA);
}
Database.update(taList, false);
}
public void finish(Database.BatchableContext Bc){
}
}
Test Class which i have written as :
-----------------------------------------------
@isTest
public class Test_DM_TechAvailibility {
@isTest
public static void Test_method() {
List<SV_Technician_Availability__c> techAvailabilityList = new List<SV_Technician_Availability__c>();
for(Integer i=0; i<10; i++) {
SV_Technician_Availability__c techAv = new SV_Technician_Availability__c();
techAv.SV_Date_String__c = '';
techAv.SV_Date__c = System.Today();
techAvailabilityList.add(techAv);
}
insert techAvailabilityList;
system.debug('2*'+techAvailabilityList);
test.startTest();
DM_TechAvailability obj = new DM_TechAvailability();
Database.executeBatch(obj);
test.stopTest();
}
}
It covers only 50% and doesn't cover 'Execute' method of apex class.
Can you please help with your inputs how can i increase the percentage?
Thanks in Advance !
Rupesh
-------------------------------------------------------------
public class DM_TechAvailability implements Database.Batchable<sObject> {
public DM_TechAvailability(){
}
public Database.QueryLocator start (Database.BatchableContext BC){
string BLANK = '';
string nulll = NULL;
String query = 'select id, SV_Date_String__c, SV_Date__c from SV_Technician_Availability__c
where SV_Date__c != null and (SV_Date_String__c =: BLANK or SV_Date_String__c =: nulll)';
system.debug('1*'+Database.getQueryLocator(query));
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List<sObject> xTcope){
List<SV_Technician_Availability__c> taList = new List<SV_Technician_Availability__c>();
for(sObject eachTcope: xTcope){
SV_Technician_Availability__c eachTA = (SV_Technician_Availability__c)eachTcope;
eachTA.SV_Date_String__c = String.ValueOf(eachTA.SV_Date__c);
taList.add(eachTA);
}
Database.update(taList, false);
}
public void finish(Database.BatchableContext Bc){
}
}
Test Class which i have written as :
-----------------------------------------------
@isTest
public class Test_DM_TechAvailibility {
@isTest
public static void Test_method() {
List<SV_Technician_Availability__c> techAvailabilityList = new List<SV_Technician_Availability__c>();
for(Integer i=0; i<10; i++) {
SV_Technician_Availability__c techAv = new SV_Technician_Availability__c();
techAv.SV_Date_String__c = '';
techAv.SV_Date__c = System.Today();
techAvailabilityList.add(techAv);
}
insert techAvailabilityList;
system.debug('2*'+techAvailabilityList);
test.startTest();
DM_TechAvailability obj = new DM_TechAvailability();
Database.executeBatch(obj);
test.stopTest();
}
}
It covers only 50% and doesn't cover 'Execute' method of apex class.
Can you please help with your inputs how can i increase the percentage?
Thanks in Advance !
Rupesh
Hi,
Please find the solution.
Please mark it as the Best Answer If your queries are solved.
Thank you
i have tried with above code but no luck..it is still the same.
Please use below code :-
DM_TechAvailability:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
thanks but when i add the above lines of code , it throws below error:
"Argument must be an inline query".
Perhaps this is not the right way to do it.
Please use it
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
If my solution is work for you then please Mark Best Answer
Regrads