Hi I am getting apex batch already scheduled error while running test class. I don't know where i am going wrong. I am calling batch class from schedule class.

Apex Class

    global class Scheduler_class_oppCountOnProjectsBatch implements Schedulable{

public static String sched = '0 00 09 * * ?'; //Every day at 9AM

global static String scheduleMe() {
   Scheduler_class_oppCountOnProjectsBatch SC = new Scheduler_class_oppCountOnProjectsBatch(); 
   return System.schedule('oppCountOnProjectsBatch Schedule Job', sched, SC);

 global void execute(SchedulableContext sc) {

  oppCountOnProjectsBatchClass ocp = new oppCountOnProjectsBatchClass();
   ID batchprocessid = Database.executeBatch(ocp,50);           

Test Class

public class Scheduler_oppCountOnProjectsBatch_test {
static testmethod void test1() {       
   String sched = '0 00 10 * * ?';
   Scheduler_class_oppCountOnProjectsBatch sch_OppCntProj = new Scheduler_class_oppCountOnProjectsBatch();
   // Scheduler_class_oppCountOnProjectsBatch.scheduleMe();
   //String schede = Scheduler_class_oppCountOnProjectsBatch.sched;
   system.schedule('Test oppCountOnProjectsBatch Schedule Job', sched, sch_OppCntProj);

 static testmethod void test2(){
  • Are tests run in parallel? At which test method/line you get this error? – kurunve Jun 1 '17 at 11:19
  • @kurunve yes tests run in parallel, i am getting error at this line Scheduler_class_oppCountOnProjectsBatch.scheduleMe(); – Effat Naaz Jun 1 '17 at 11:31
  • Effat, you can try running your test class by disabling Parallel test running. You can do it from screen "Apex Test Execution" from Setup. – Nitish Singhal Jun 1 '17 at 11:39
  • 1
    I got similar issues before. in case if you run tests, then sometimes they may take some things from environment(aka users etc). As long as there is scheduled job within environment with same name, unit tests may fail, because it is impossible to have to scheduled jobs with same names. Personally I made a logic to give another job name (with suffix) to ensure that unit tests does not interfere with environments(s) – kurunve Jun 1 '17 at 11:39
  • @NitishSinghal I have already done this. – Effat Naaz Jun 1 '17 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.