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
RAJNagRAJNag 

I have onemore doubt is without calling Cron Method How can i cover code coverage in execute method it is possible .

Hi

@isTest 
public class BatchDeleteEndUserTest 
{
    static testMethod void TestBatchDeleteEndUser() 
    {
        
        // Create End User data here, please update that as per your object and fields
        EndUser__c objEndUser = new EndUser__c();
        objEndUser.Name = 'Test End User';
        Insert objEndUser;
        
         batchDeleteEndUser obj = new batchDeleteEndUser();   
         String chron = '0 0 23 * * ?';        
         String jobid = System.schedule('testScheduledApex', chron, obj);
         CronTrigger ct = [Select id , CronExpression from CronTrigger where id = :jobId];
         System.assertEquals(chron,ct.CronExpression); 
    }
}


Class
global class batchDeleteEndUser implements Schedulable
{
    
    public static String CRON_EXP = '0 0 12 1 1/3 ? *';
 
    
    global static String scheduleIt() {
        batchDeleteEndUser delbatch = new batchDeleteEndUser();
        return System.schedule('Delete EndUser Batch', CRON_EXP, delbatch);
        } 
    
 
    
    global void execute(SchedulableContext sc) {
        
        Datetime days = system.now().addDays(-365);
        
       List<EndUser__c> objectList = new List<EndUser__c>();
         
        for (EndUser__c EUser: [
            select Id,name
            from EndUser__c
            where Id not in (select End_User__c from Case where status != 'Closed') and ((Time_of_Last_Created_Case__c<= :days and Time_of_Last_Initiated_Chat__c<=:days ) or (Time_of_Last_Created_Case__c=null and Time_of_Last_Initiated_Chat__c<=:days) or (Time_of_Last_Created_Case__c<= :days and Time_of_Last_Initiated_Chat__c=null))
        ]) {
            objectList.add(EUser);
           
        }
       
 
        if (!objectList.isEmpty()) {
           delete objectList;
           system.debug('enduser----'+objectList);
        }
  }  
}


Regards 

Raj

 
MandyKoolMandyKool
Hi,

If my understanding is correct; you want to cover your test method and as you are setting up cron expression it is not executing your business logic as the call is asynchronous.

When writing test methods for asynchronous apex you should make sure that the code calling your asyn. method is inside Test.startTest() and Test.stopTest() methods.

Simply wrap your test method code inside Test.startTest() and Test.stopTest() and it will resolve your issue.
Amit Chaudhary 8Amit Chaudhary 8

For scheduler you need cron method. But that will better if you use Batch job for bulk job not the scheduler. In that case you can use below code in test class to call batch job:-

batchDeleteEndUser delbatch = new batchDeleteEndUser();
Database.executeBatch(delbatch ,200);

Please check below post for batch job:-
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
http://blog.shivanathd.com/2013/01/how-to-write-batch-class-in.html

Please let us know if this will help you.

Thanks
Amit Chaudhary
Amit Chaudhary 8Amit Chaudhary 8

Please let us know if your issue is resolved or you required some more info