function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhishek Singh 88Abhishek Singh 88 

increase test coverage

Hi Developers.
Here i have written a class
Global class Email_History_schedule implements system.Schedulable
{
     global void execute(SchedulableContext sc)
     {
         try
         {
            List<Email_to_incident_History__c>emailtoincident=new List<Email_to_incident_History__c>();
             for(Email_to_incident_History__c einci:[select id from Email_to_incident_History__c])
             {
                 emailtoincident.add(einci);
             }
             delete emailtoincident;
             system.debug('Email history List'+emailtoincident);
         }
         catch(exception ex)
         {
             
         }
     }
}

And here is my test class
@istest
public class Email_History_schedule_Test 
{
   public static testMethod void testschedule() {
Test.StartTest();
Email_to_incident_History__c abc= new Email_to_incident_History__c();
abc.account__c='Mckesson';
abc.Description__c='Success';
insert abc;
List<Email_to_incident_History__c> emailto=new List<Email_to_incident_History__c>();
 emailto.add(abc);
 delete emailto;
 Email_History_schedule sh1 = new Email_History_schedule();
String sch = '0 0 23 * * ?';
system.schedule('CHS SR Report', sch, sh1);
Test.stopTest();
}   
}

But it is covering only 62%.
This line is not been covered
{
                 emailtoincident.add(einci);

 
Best Answer chosen by Abhishek Singh 88
Akshay_DhimanAkshay_Dhiman
Hi Abhishek,
 Try below code it will help code coverage to increase
   
@istest
   public class Email_History_schedule_Test 
   {
     public static testMethod void testschedule() {
       Test.StartTest();
         Email_to_incident_History__c abc= new Email_to_incident_History__c();
           abc.account__c='Mckesson';
           abc.Description__c='Success';
         insert abc;
         List<Email_to_incident_History__c> emailto=new List<Email_to_incident_History__c>();
           emailto.add(abc);
           Email_History_schedule sh1 = new Email_History_schedule();
           String sch = '0 0 23 * * ?';
           System.schedule('CHS SR Report', sch, sh1);
       Test.stopTest();
      }   
   }
 
The reason your line " emailtoincident.add(einci); " is not covered because in your test class in line number 12 you are deleting the record that you have created so when there will no record on Email_to_incident_History__c , there will no record inside your list and won't be entering inside the for loop.
Please mark my answer as a solution if it is helpful.
Regards,
Akshay
 

All Answers

Akshay_DhimanAkshay_Dhiman
Hi Abhishek,
 Try below code it will help code coverage to increase
   
@istest
   public class Email_History_schedule_Test 
   {
     public static testMethod void testschedule() {
       Test.StartTest();
         Email_to_incident_History__c abc= new Email_to_incident_History__c();
           abc.account__c='Mckesson';
           abc.Description__c='Success';
         insert abc;
         List<Email_to_incident_History__c> emailto=new List<Email_to_incident_History__c>();
           emailto.add(abc);
           Email_History_schedule sh1 = new Email_History_schedule();
           String sch = '0 0 23 * * ?';
           System.schedule('CHS SR Report', sch, sh1);
       Test.stopTest();
      }   
   }
 
The reason your line " emailtoincident.add(einci); " is not covered because in your test class in line number 12 you are deleting the record that you have created so when there will no record on Email_to_incident_History__c , there will no record inside your list and won't be entering inside the for loop.
Please mark my answer as a solution if it is helpful.
Regards,
Akshay
 
This was selected as the best answer
Abhishek Singh 88Abhishek Singh 88
Thanks,
Can u Please tell me that why you removed "delete emailto";
Akshay_DhimanAkshay_Dhiman
Hi Abhishek,

As you are writing a test class for any class remember you have to assume that your dev-org is empty and you have create records for the Standard as well as Custom Sobject through coding which you have done but as you are writing that "delete emailto" command you are deleting the record which you have just created.That's why your code coverage can not increase because its not entering inside the for loop so I have removed "delete emailto" and result of that is there is a record in Email_to_incident_History__c custom object and it could entry the for loop and inside code could also be covered and increase your code coverage.

Regards,
Akshay.