You need to sign in to do that
Don't have an account?
Execute Method not Code Covered. What am I doing wrong?
Hello All,
I have been struggling with my APEX code to delete Shipment records then send and email. The APEX code works correctly when tested and receive the email with results, however I cannot get above 66% Code Coverage when I test. Lines in BOLD are not code covered? I am losing my mind and my boss is angry. Please Help.
With thanks in advance.
Robert
/*
* Description : This is Batch to Perform delete operation on Shipment Records.
*
* Shipments are Deletable when Shipment is 42 days or older.
* Formula "TODAY() > Date__c + 42" sets Deletable__c = TRUE
*
* Created By : Robert Wambold
*
* Created Date : 08/09/2018
*
* Version : v1.0 - Create
*/
//Batch Class
global class Batch_ShipmentDelete implements Database.Batchable<sObject>,Database.stateful {
//Variable to count Success and Error Records
public Integer successCounter = 0;
public Integer failureCounter = 0;
//Start Method
global Database.QueryLocator start(Database.BatchableContext BC) {
//Query to Fetch Records
return Database.getQueryLocator([SELECT ID FROM Shipment__c WHERE Deletable__c = TRUE]);
}
//Execute Method
global void execute(Database.BatchableContext BC, List<Shipment__C> scope) {
//Delete the Records those are in Contexts
Database.DeleteResult[] delresults = Database.delete((scope),false);
//Loop through records going to be deleted
for(Database.DeleteResult dr : delResults){
//If record is not successfully deleted
if(!dr.isSuccess()){
//List to hold Error
Database.Error[] errs = dr.getErrors();
//Loop through list of Error
for(Database.Error err : errs) {
//Debug to get Error Status
System.debug('$#$#$#$' + err.getStatusCode() + ' - ' + err.getMessage());
//Debug to get Error Message
System.debug('ErrorMessage##########'+ err.getMessage());
}
//Increase counter for Failure
failureCounter++;
}
else {
successCounter++;
}
}
}
//Finish Method
global void finish(Database.BatchableContext BC){
// Query the AsyncApexJob object to retrieve the current job's information.
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob WHERE Id = :BC.getJobId()];
// Send an email to the Apex job's submitter notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Shipment Delete Job ' + a.Status);
mail.setPlainTextBody
('Apex Shipment Delete Job has been completed. There were total of ' + a.TotalJobItems
+ ' batches with ' + successCounter + ' success and ' + failureCounter
+ ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}
}
My Test Code
@IsTest
public class Batch_ShipmentDeleteTEST
{
//Database.Error[] errs = dr.getErrors();
static testMethod void testMethod1()
{
Test.startTest();
Batch_ShipmentDelete obj = new Batch_ShipmentDelete();
DataBase.executeBatch(obj);
Test.stopTest();
}
//Database.Error[] errs = dr.getErrors();
static testMethod void testMethod2()
{
Test.startTest();
Batch_ShipmentDelete obj = new Batch_ShipmentDelete();
DataBase.executeBatch(obj);
Test.stopTest();
}
}
You will need to create the test data for the batch class to process, the Shipment__c record with the Deletable__c as true. Also when calling the batch class from test class use batch size as 1.
Regards,
Lazy Coder
Thank you! With code below I hit 87% Code Coverage..
@IsTest
public class Batch_ShipmentDeleteTEST
{
//Database.Error[] errs = dr.getErrors();
static testMethod void testMethod1()
{
List <Shipment__c> lstShipment = new List<Shipment__c>();
Shipment__c testShipment = new Shipment__c();
//testShipment.Name='Test Shipment' ;
testShipment.Pickup_Date__c=date.parse('02/18/2018');
lstShipment.add(testShipment);
Shipment__c testShipment1 = new Shipment__c();
//testShipment1.Name='Test Shipment1' ;
testShipment1.Pickup_Date__c=date.parse('02/18/2018');
lstShipment.add(testShipment1);
insert lstShipment;
Test.startTest();
Batch_ShipmentDelete obj = new Batch_ShipmentDelete();
DataBase.executeBatch(obj);
Test.stopTest();
}
//Database.Error[] errs = dr.getErrors();
static testMethod void testMethod2()
{
Test.startTest();
Batch_ShipmentDelete obj = new Batch_ShipmentDelete();
DataBase.executeBatch(obj);
Test.stopTest();
}
}