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
JaanuJaanu 

Test Class for apex batchable class ?

I have the following class ...When I am trying to schedule this apex class ... not able to find it ... thinking that it's bec there is zero test coverage .. am i correct on that ? If so, how to write test class to get test coverage 100%.. 

When I tried to schedule the same class in DEV, I was able to schedule it ... I did not have any test class also ... thanks.

global class DeleteRecords implements Schedulable{ global void execute(SchedulableContext SC) { List<Custom_Object__c> customObj = [select Id FROM Custom_Object__c WHERE createddate = LAST_N_DAYS:30]; if(!customObj.isEmpty()) delete customObj; } }

 
Raj VakatiRaj Vakati
Try this

 
@isTest 
public class DeleteRecordsTest 
{
    static testMethod void testMethod1() 
    {
        List<Custom_Object__c > lstCO= new List<Custom_Object__c >();
        for(Integer i=0 ;i <200;i++)
        {
            Custom_Object__c  cp = new Custom_Object__c ();
// Add other reuqired fields
            cp.Name ='Name'+i;
            lstLead.add(cp);
        }
        
        insert lstCO;
        
        Test.startTest();

            DeleteRecords  obj = new DeleteRecords ();
            String sch = '0 0 23 * * ?';
			system.schedule('Test Delete status Check', sch, obj);
            
        Test.stopTest();
    }
}

 
v varaprasadv varaprasad
Hi Jaanu,

Try this : 
 
@isTest
        private class DeleteRecordsTest{
        static testmethod void  testschedule(){
		
		list<Custom_Object__c> lstObjects = new list<Custom_Object__c>();
        for(integer i = 0; i<=200; i++){
		    //Add all required fields here
		   lstObjects.add(new Custom_Object__c(name = 'Test'+i));
		}
		
		insert lstObjects;
		
        Test.StartTest();
			 DeleteRecords  sh1 = new DeleteRecords ();      
			 String sch = '0  00 1 3 * ?';
			 system.schedule('Test', sch, sh1);         
        Test.stopTest();
     }
    }

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Thanks
Varaprasad
Salesforce Freelance Consultant/Developer/Administrator
@For Salesforce Project Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1