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 

Please anyone write the Test Class

Hi 

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);
        }
  }  
}


Hi any one write the test class for above class its very urgent please---------------------

Thanks for Advance

Raj
Best Answer chosen by RAJNag
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class:-
@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); 
	}
}

Please let us know if this will help you.

Thanks,
Amit Chaudhary

All Answers

Jigar.LakhaniJigar.Lakhani

Hello,

Please try with below test class.

@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 objBatchDeleteEndUser = new BatchDeleteEndUser();
		objBatchDeleteEndUser.execute(null);
		
	}
}
 

Thanks & Cheers,
Jigar(pateljb90@gmail.com)

Amit Chaudhary 8Amit Chaudhary 8
Please try below test class:-
@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); 
	}
}

Please let us know if this will help you.

Thanks,
Amit Chaudhary
This was selected as the best answer
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11
HI Rajnag,
Pls try this below code

@isTest 
public class BatchDeleteEndUserTest 
{
    static testMethod void TestBatchDeleteEndUser() 
    {
        Test.startTest();
        // 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.scheduleIt();
        
         batchDeleteEndUser obj = new batchDeleteEndUser();   
         String chron = '0 0 23 * * ?';        
          System.schedule('testScheduledApex', chron, obj);
     Test.stopTest();
         
    }
}


If you get the answer, please mark it as the correct answer. It will be a help to others who are facing the same problem later.

Regards
Eswar Prasad
RAJNagRAJNag
Hi amith 

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


Regards
Raj
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);