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
DritterDritter 

System.UnexpectedException: No more than one executeBatch can be called from within a testmethod

I'm running batch apex to deactivate portal users every night, so we don't limit are limit on licenses. I'm getting this Exception on my test class when pushing the code to production. 

System.UnexpectedException: No more than one executeBatch can be called from within a testmethod

I found the following article which seems to have the solution, but I'm not sure where exactly to place the code block:

https://help.salesforce.com/apex/HTViewSolution?id=000176562&language=en_US

Here's the test class:

@isTest
private class KapUserCleanUpSchedulerTest {
public static testmethod void test() {
   
   Test.startTest();

   // Schedule the test job 
      String jobId = System.schedule('testMassScheduledApexonet', KapUserCleanUpScheduler.CRON_EXP, new KapUserCleanUpScheduler());
   // Get the information from the CronTrigger API object 
      CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];

   // Verify the expressions are the same 
      System.assertEquals(KapUserCleanUpScheduler.CRON_EXP, ct.CronExpression);
   // Verify the job has not run 
      System.assertEquals(0, ct.TimesTriggered);

   // Verify the next time the job will run 
      System.assertEquals('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));

      public void finish(Database.BatchableContext);
         {
            if(!Test.isRunningTest)
               Database.executeBatch(new MySecondBatchJob));
         }
      
   Test.stopTest();
   
      }
   }

I'm new to coding, so any help is appreciated. Thanks!
Waqar Hussain SFWaqar Hussain SF
Hello Dritter, 
please have a look to this article it really helps you, it will solve your problem.
let me know if it works,
https://help.salesforce.com/apex/HTViewSolution?id=000176562&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000176562&language=en_US)
DritterDritter
Hi Vickey, I included a link to this article in my post. I added the block of code, but I'm getting a compile error when I try to save it. I'm not sure I'm putting it in the right spot.