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
sfdc-Lsfdc-L 

how do i write test class for batch apex scheduler?

hi all

 

in the batch apex sheduler,i did n't provide system.scheduler like data and time,i have mentioned as manually such as follows,

 

batchapexscheduler:

----------------------------

sambatchapexscheduler  testbas=new sambatchapexscheduler();

database.executebatch(testbas,200);

 

so how do i write test class for above and please let me know asap

Thanks!

 

crop1645crop1645

This is covered in detail in the Apex Developer's Guide - section Apex Scheduler

 

When you are testing the call to System.schedule, the actual Cron exp[ression you use can be almost anything as the test wraps the call to System.schedule with a test.startTest(), Test.stoptest(). 

 

In a test context, the schedulable class will execute as soon as Test.stopTest() is encountered, regardless of the actual CRON expression. Note in the example that a date far in the future is used in the test method.

 

In my code, I have  1+ separate test methods for the class that implements Schedulable -- they test the class assuming it has been scheduled.  Then, just like in the Apex doc example, I have a separate testmethod to verify that the class gets scheduled and some trivial test to prove that the scheduled class runs.  This way, you can keep your test code compartmentalized

 

..many methods to test the business logic of your schedulable class

.. 1 method to verify that you can schedule

 

sfdc-Lsfdc-L
thanks eric, Can you please provide the test method for this effort that will be help full
crop1645crop1645

sfdc_l

 

I think I'm confused about your scenario --  do you have a batchable APEX class that you want to run as a scheduled job? If so, you're going to need more than 2 lines of code.  Please review the SFDC Apex doc on batchable classes as well as schedulable classes - these are two different concepts.  Each can be tested independent of each other.

 

To make the problem simpler for yourself, work on the schedulable class first that simply executes a dummy method on a dummy class.  Then develop the test code for the batchable class without worryng how to schedule it. Finally, replace your call to the dummy method with an object instantiation of the batchable class followed by Database.executeBatch(my BatchableObject);

 

 

sfdc-Lsfdc-L
Yes eric, it is batch apex and when i was trying to write test class getting error as 'External entry point' Test.StartTest(); sambatchapexscheduler testbas=new sambatchapexscheduler(); string sche='0 0 13 **?'; system.schedule('test'sche,testbas); test.stopTest(); Please let me know the solution asap. Thanks!
maity.keshabmaity.keshab

// Extension

Account testAccount = new Account(Name='Test Company Name123');
insert testAccount;
ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
newAccountPlan testAccPlan = new newAccountPlan(sc);

//VF Pages
PageReference pageRef = Page.AccountPlan;
pageRef.getParameters().put('id', value);
//pageRef.getParameters().put('id', String.valueOf(testAccountPlanInsert.Id));
Test.setCurrentPage(pageRef);
anyVariable = ApexPages.currentPage().getParameters().get('AnyVariable');
ApexPages.currentPage().getParameters().put('AnyVariable',value);

//RunAS
Profile p = [select id from profile where name='Standard User'];
User u = new User(alias = 'standt', email='standarduser@testorg.com', emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
System.runAs(u) {}
//for large data set
test.startTest();
// Insert the Account records that cause the trigger to execute.
insert accounts;
// Stop the test, this changes limit context back to test from trigger.
test.stopTest();


///Test Methods and Bulk Operations
Test.startTest();
insert contactsToCreate;
// list of contact;
Test.stopTest();

//The .getURL will return the page url the Save() method returns. String nextPage = controller.save().getUrl();


// Batch
ID batchprocessid = Database.executeBatch(myClass);
ID batchprocessid = Database.executeBatch(myClass, batchsize);


//Scheduler
String jobId = System.schedule('jobname',CRON_EXP,object );



//Test Methods and Apex Callouts
static testMethod void testWebService(){ //First, build the http request
HttpRequest req = buildWebServiceRequest();
//NOTE - WE DO NOT EXECUTE THE METHOD, invokeWebService.
//Now, since we can't execute the actual web service, //write apex code to build a sample HttpResponse object
HttpResponse res = new HttpResponse();
//Apply test data and attributes to the HttpResponse object as needed
handleWebServiceResponse(res);
}