You need to sign in to do that
Don't have an account?
![ManoharSF ManoharSF](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Test coverage for batchable class
Hi there,
How can i get 100% coverage for this code? I get only 55% so far..
Any help is greatly appreciated.
Thank you,
Manohar
global class ISMTrackingBatchProcess implements Database.Batchable < sObject > { global Database.QueryLocator start(Database.BatchableContext ctx) { System.Debug('***** Here it starts *****'); String Query = 'Select Owner_Name__c, Contact_Name__c,CaseNumber, Owner_Email__c, Dummy_ISM_Datetime__c, TFS_ID__c, Id, ISM_Priority__c, ISM_Priority_Set_Critical_High_At__c From Case where Dummy_ISM_Datetime__c <> Null and ISM_Priority_Set_Critical_High_At__c <> Null and TFS_ID__c <> Null and ISM_Priority__c in (\'Critical\',\'High\')' ; return Database.getQueryLocator(Query); } global void execute(Database.BatchableContext ctx, List<sObject> scope) { integer failedUpdates1; integer failedUpdates2; List<Case> cs= (List<Case>)scope; List<ISM_Communication_Tracker__c> ismcts = new List<ISM_Communication_Tracker__c>(); List<ISM_Communication_Alert__c> ismcas = new List<ISM_Communication_Alert__c>(); boolean missflg ; integer missflg10; if (cs.size()>0) { for(Integer i = 0; i < cs.size(); i++){ if (cs[i].ISM_Priority__c == 'Critical' ) { // Send Email ISM Alert Emails integer dmincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].Dummy_ISM_Datetime__c.getTime() )) / (1000.0*60.0*60.0)).intValue(); for (integer j = 0; j < dmincr; j++){ //Test not covered from here onwards task[] t =[select id from task where whatId = :cs[i].id and createddate >= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j) and createddate <= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(30) and Subject in ('Call','Email') limit 1]; if (t.size() == 0 ) { ismcas.add(new ISM_Communication_Alert__c(Created_Date_and_Time__c = System.now() , Case_Owner_Email__c =cs[i].Owner_Email__c, ISM_Priority__c =cs[i].ISM_Priority__c, Unique_index__c =cs[i].CaseNumber +string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j)) + string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(30)), Case__c = cs[i].id, Log_start_at__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j), Log_End_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(30), of_Emails__c = j)); } } ////Test not covered until here // Create ISM Communication Logs integer hrincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)).intValue(); for (integer j = 0; j < hrincr; j++){ //Test not covered from here onwards task[] t =[select id from task where whatId = :cs[i].id and createddate >= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j) and createddate <= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1) and Subject in ('Call','Email') limit 1]; if (t.size() > 0 ) { missflg = false; missflg10 = 0; } else { missflg = true; missflg10 = 1; } ismcts.add(new ISM_Communication_Tracker__c(Case__c = cs[i].id, Missed_Communication__c = missflg, Missed_Communication1_0__c = missflg10, Log_Start_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j), PrimaryKey__c = string.valueOf(cs[i].id) + string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c) + cs[i].ISM_Priority__c + string.valueof(j) , ISM_Priority__c = cs[i].ISM_Priority__c, ISM_Priority_Set_Critical_High_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c, Logged_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1), of_hrs_since_ISM_Priority_Chnaged__c =Math.Floor(Decimal.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1).getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0) )); } } ////Test not covered until here Else if (cs[i].ISM_Priority__c == 'High' ) { // Send Email ISM Alert Emails integer dmincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].Dummy_ISM_Datetime__c.getTime() )) / (1000.0*60.0*60.0)).intValue(); for (integer j = 0; j < dmincr; j=j+4){ //Test not covered from here onwards task[] t =[select id from task where whatId = :cs[i].id and createddate >= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j) and createddate <= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(150) and Subject in ('Call','Email') limit 1]; if (t.size() == 0 ) { ismcas.add(new ISM_Communication_Alert__c(Created_Date_and_Time__c = System.now() , Case_Owner_Email__c =cs[i].Owner_Email__c, ISM_Priority__c =cs[i].ISM_Priority__c, Unique_index__c =cs[i].CaseNumber +string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j)) + string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(150)), Case__c = cs[i].id, Log_start_at__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j), Log_End_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(210), of_Emails__c = j)); } } ////Test not covered until here //Create ISM Communication Tracking Log integer hrincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)).intValue(); for (integer j = 3; j < hrincr; j=j+4){ //Test not covered from here onwards task[] t =[select id from task where whatId = :cs[i].id and createddate >= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j-3) and createddate <= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1) and Subject in ('Call','Email') limit 1]; if (t.size() > 0 ) { missflg = false; missflg10 = 0; } else { missflg = true; missflg10 = 1; } ismcts.add(new ISM_Communication_Tracker__c(Case__c = cs[i].id, Missed_Communication__c = missflg, Missed_Communication1_0__c = missflg10, Log_Start_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j-3), PrimaryKey__c = string.valueOf(cs[i].id) + string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c) + cs[i].ISM_Priority__c + + string.valueof(j-3) , ISM_Priority__c = cs[i].ISM_Priority__c, ISM_Priority_Set_Critical_High_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c, Logged_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1), of_hrs_since_ISM_Priority_Chnaged__c =Math.Floor(Decimal.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1).getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0) )); } } } failedUpdates1 = 0; List<Database.UpsertResult> dsrs1 = Database.upsert(ismcts, false); for(Database.UpsertResult dsr1 : dsrs1){ if(!dsr1.isSuccess()){ failedUpdates1++; ////Test not covered until here } } failedUpdates2 = 0; List<Database.UpsertResult> dsrs2 = Database.upsert(ismcas, false); for(Database.UpsertResult dsr2 : dsrs2){ if(!dsr2.isSuccess()){ failedUpdates2++; } } } } global void finish(Database.BatchableContext ctx) { System.debug(LoggingLevel.WARN,'Batch Process Finished'); } }
My Test Class so far...
@isTest private class testscheduleCriticalISM{ public static testMethod void testscheduleISMTrackingBatchProcess() { Test.startTest(); scheduleISMTrackingBatchProcess s = new scheduleISMTrackingBatchProcess(); string sch = '0 0 * * 1-12 ? *'; system.schedule('Process Trans 1', sch, s); Test.stopTest(); } public static testMethod void testISMTrackingBatchProcess() { //get the initial details Profile pf = [Select Id from Profile where Name = 'System Administrator']; //creating RunAs User Record User u = new User(); u.FirstName = 'Test'; u.LastName = 'user'; u.Email = 'testuser@test123456789.com'; u.CompanyName = 'test.com'; u.Title = 'Test user'; u.Username = 'testuser@test123456789.com'; u.Alias = 'testuser'; u.CommunityNickname = 'Test User'; u.TimeZoneSidKey = 'America/Chicago'; u.LocaleSidKey = 'en_US'; u.EmailEncodingKey = 'ISO-8859-1'; u.ProfileId = pf.Id; u.LanguageLocaleKey = 'en_US'; insert u; // Datetime dToday = datetime.newInstance(MySystem.SetToday().year(), MySystem.SetToday().month(),MySystem.SetToday().day()); Datetime origDate = DateTime.valueOf('2013-01-01 0:0:0'); Datetime nowDatetime = DateTime.valueOf('2013-01-01 12:0:0'); String ISM_Priority; List <Case> kases = new List<Case>(); for(integer i = 0; i<200; i++){ if (i < 100) { ISM_Priority = 'Critical';} else { ISM_Priority = 'High';} Case c = new Case(Subject='testCase'+'i', Ownerid = u.ID, Status = 'Open', Origin = 'Other', TFS_ID__c = i, ISM_Priority_Set_Critical_High_At__c = origDate, Dummy_ISM_Datetime__c = origDate.addMinutes(-30), ISM_Priority__c = ISM_Priority ); kases.add(c); } insert kases; task[] t =[select id from task where Subject in ('Call','Email') ]; integer hrincr = (Math.Floor(decimal.valueOf(nowDatetime.getTime() - origDate.getTime() )) / (1000.0*60.0*60.0)).intValue(); integer dmincr = (Math.Floor(decimal.valueOf(nowDatetime.getTime() - origDate.addMinutes(-30).getTime() )) / (1000.0*60.0*60.0)).intValue(); Test.startTest(); ISMTrackingBatchProcess b = new ISMTrackingBatchProcess(); ID myBatchJobID = database.executebatch(b); if (t.size() ==0 && hrincr > 4) { System.AssertEquals(database.countquery('SELECT COUNT() FROM ISM_Communication_Alert__c WHERE ISM_Priority__c =\'Critical\''), 0); } if (t.size() ==0 && hrincr > 4) { System.AssertEquals(database.countquery('SELECT COUNT() FROM ISM_Communication_Tracker__c WHERE ISM_Priority__c =\'Critical\' and Missed_Communication__c = true'), 0); } Test.stopTest(); } }
Check this following four lines. May be data is not populating through test method so that it meet the requirements of those for loops i.e they may be returning value less than 0.
1) integer dmincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].Dummy_ISM_Datetime__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
2) integer hrincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
3) integer dmincr = (Math.Floor
(decimal.valueOf(Datetime.now().getTime() - cs[i].Dummy_ISM_Datetime__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
4) integer hrincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
All Answers
Can you mark the part not covered presently?
Hi Souvik,
Please see below, the one in bold is not covered.
Thank you,
Manohar
global class ISMTrackingBatchProcess implements Database.Batchable < sObject >
{
global Database.QueryLocator start(Database.BatchableContext ctx)
{
System.Debug('***** Here it starts *****');
String Query = 'Select Owner_Name__c, Contact_Name__c,CaseNumber, Owner_Email__c, Dummy_ISM_Datetime__c, TFS_ID__c, Id, ISM_Priority__c, ISM_Priority_Set_Critical_High_At__c From Case where Dummy_ISM_Datetime__c <> Null and ISM_Priority_Set_Critical_High_At__c <> Null and TFS_ID__c <> Null and ISM_Priority__c in (\'Critical\',\'High\')' ;
return Database.getQueryLocator(Query);
}
global void execute(Database.BatchableContext ctx, List<sObject> scope)
{
integer failedUpdates1;
integer failedUpdates2;
List<Case> cs= (List<Case>)scope;
List<ISM_Communication_Tracker__c> ismcts = new List<ISM_Communication_Tracker__c>();
List<ISM_Communication_Alert__c> ismcas = new List<ISM_Communication_Alert__c>();
boolean missflg ;
integer missflg10;
if (cs.size()>0)
{
for(Integer i = 0; i < cs.size(); i++){
if (cs[i].ISM_Priority__c == 'Critical' )
{
// Send Email ISM Alert Emails
integer dmincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].Dummy_ISM_Datetime__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
for (integer j = 0; j < dmincr; j++){
//Test not covered from here onwards
task[] t =[select id from task where whatId = :cs[i].id and createddate >= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j) and createddate <= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(30) and Subject in ('Call','Email') limit 1];
if (t.size() == 0 )
{
ismcas.add(new ISM_Communication_Alert__c(Created_Date_and_Time__c = System.now() ,
Case_Owner_Email__c =cs[i].Owner_Email__c,
ISM_Priority__c =cs[i].ISM_Priority__c,
Unique_index__c =cs[i].CaseNumber +string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j)) + string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(30)),
Case__c = cs[i].id,
Log_start_at__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j),
Log_End_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(30),
of_Emails__c = j));
}
}
////Test not covered until here
// Create ISM Communication Logs
integer hrincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
for (integer j = 0; j < hrincr; j++){
//Test not covered from here onwards
task[] t =[select id from task where whatId = :cs[i].id and createddate >= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j) and createddate <= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1) and Subject in ('Call','Email') limit 1];
if (t.size() > 0 )
{
missflg = false;
missflg10 = 0;
}
else
{
missflg = true;
missflg10 = 1;
}
ismcts.add(new ISM_Communication_Tracker__c(Case__c = cs[i].id,
Missed_Communication__c = missflg,
Missed_Communication1_0__c = missflg10,
Log_Start_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j),
PrimaryKey__c = string.valueOf(cs[i].id) + string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c) + cs[i].ISM_Priority__c + string.valueof(j) ,
ISM_Priority__c = cs[i].ISM_Priority__c,
ISM_Priority_Set_Critical_High_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c,
Logged_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1),
of_hrs_since_ISM_Priority_Chnaged__c =Math.Floor(Decimal.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1).getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)
));
}
}
////Test not covered until here
Else if (cs[i].ISM_Priority__c == 'High' )
{
// Send Email ISM Alert Emails
integer dmincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].Dummy_ISM_Datetime__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
for (integer j = 0; j < dmincr; j=j+4){
//Test not covered from here onwards
task[] t =[select id from task where whatId = :cs[i].id and createddate >= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j) and createddate <= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(150) and Subject in ('Call','Email') limit 1];
if (t.size() == 0 )
{
ismcas.add(new ISM_Communication_Alert__c(Created_Date_and_Time__c = System.now() ,
Case_Owner_Email__c =cs[i].Owner_Email__c,
ISM_Priority__c =cs[i].ISM_Priority__c,
Unique_index__c =cs[i].CaseNumber +string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j)) + string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(150)),
Case__c = cs[i].id,
Log_start_at__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j),
Log_End_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j).addMinutes(210),
of_Emails__c = j));
}
}
////Test not covered until here
//Create ISM Communication Tracking Log
integer hrincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
for (integer j = 3; j < hrincr; j=j+4){
//Test not covered from here onwards
task[] t =[select id from task where whatId = :cs[i].id and createddate >= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j-3) and createddate <= :cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1) and Subject in ('Call','Email') limit 1];
if (t.size() > 0 )
{
missflg = false;
missflg10 = 0;
}
else
{
missflg = true;
missflg10 = 1;
}
ismcts.add(new ISM_Communication_Tracker__c(Case__c = cs[i].id,
Missed_Communication__c = missflg,
Missed_Communication1_0__c = missflg10,
Log_Start_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j-3),
PrimaryKey__c = string.valueOf(cs[i].id) + string.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c) + cs[i].ISM_Priority__c + + string.valueof(j-3) ,
ISM_Priority__c = cs[i].ISM_Priority__c,
ISM_Priority_Set_Critical_High_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c,
Logged_At__c = cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1),
of_hrs_since_ISM_Priority_Chnaged__c =Math.Floor(Decimal.valueOf(cs[i].ISM_Priority_Set_Critical_High_At__c.addhours(j+1).getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)
));
}
}
}
failedUpdates1 = 0;
List<Database.UpsertResult> dsrs1 = Database.upsert(ismcts, false);
for(Database.UpsertResult dsr1 : dsrs1){
if(!dsr1.isSuccess()){
failedUpdates1++;
////Test not covered until here
}
}
failedUpdates2 = 0;
List<Database.UpsertResult> dsrs2 = Database.upsert(ismcas, false);
for(Database.UpsertResult dsr2 : dsrs2){
if(!dsr2.isSuccess()){
failedUpdates2++;
}
}
}
}
global void finish(Database.BatchableContext ctx)
{
System.debug(LoggingLevel.WARN,'Batch Process Finished');
}
}
Check this following four lines. May be data is not populating through test method so that it meet the requirements of those for loops i.e they may be returning value less than 0.
1) integer dmincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].Dummy_ISM_Datetime__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
2) integer hrincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
3) integer dmincr = (Math.Floor
(decimal.valueOf(Datetime.now().getTime() - cs[i].Dummy_ISM_Datetime__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
4) integer hrincr = (Math.Floor(decimal.valueOf(Datetime.now().getTime() - cs[i].ISM_Priority_Set_Critical_High_At__c.getTime() )) / (1000.0*60.0*60.0)).intValue();
yep thats another thing that I am dealing with Datetime.now(), how do I mock up today() or now() in test class?
I tried using this but doesnt work same.
Any ideas?
Thank you,
Manohar