0

I have two different scheduled classes that I am trying to schedule from a single test class. A simplification is the first class creates a set of records, and then the second class does something else with those records.

I can write a test class for either of them, but since they are so closely linked I thought it would be good to create a single test class that tests them both.

First When I scheduled the job with String jobId = System.schedule('ScheduledApexTest',CRON_EXP, new exampleClass()); and then stop the test with Test.stopTest();, nothing happens. The TimesTriggered stays 0. This is kind of a separate issue, but I've been testing using the method below.

test.startTest();

SchedulableContext sc;
exampleClass1 EC1 = new exampleClass1();
EC.execute(sc);

SchedulableContext sc2;
exampleClass2 EC2 = new exampleClass2();
EC2.execute(sc2);

test.stopTest();

For some reason when I do this, it seems to run exampleClass1 twice and doesn't run exampleClass2. I also tried using only one schedulablecontext in both calls, but that gives a similar error.

Each of them works by themselves, but it acts weird when I do both together. Is this something that can be done, or do I just have to do this in two different test classes?

3
  • are the execute() methods delegating work to further async transactions? The above should work fine
    – cropredy
    Aug 11 at 22:40
  • Why would you not test them separately?
    – Adrian Larson
    Aug 11 at 23:37
  • My first scheduled class is a custom lead conversion, and the second one creates opportunities based on lead and contact fields. I wanted the test records for the opp builder to exactly mirror what was created from the lead converter. But I didn't want to put the whole lead converter in the test class, and I'm struggling to reference it for the same reasons above. Aug 12 at 16:12
1

You can really only test one asynchronous thing per unit test. Use two unit tests. Both unit test methods can be in the same class, and you can use @TestSetup if you want to set up data common to both tests in order to preserve execution time/governor limits. If you do decide to use @TestSetup, keep also in mind that @TestSetup will eat up your unit test governor limits unless you call Test.startTest() first, so make sure you do that.

2
  • When I try this I get an error saying "No more than one executeBatch can be called from within a test method". The documentation usually says this happens when having more than 200 records in a batch, but that's definitely not the case here so I think each class being batchable is also an issue now. Aug 12 at 18:43
  • @denvergreene It can also happen if two batches are called in the same test method. They need to be called in separate unit tests.
    – sfdcfox
    Aug 12 at 19:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.