12

I've seen plenty of posts and documentation on testing asynchronous apex in general, but what about code with multiple layers of asynchronous code?

Specifically, I have a scheduled apex class which contains an @future method. When I test the @future method by itself, I get expected results. However, when I set up a test on the schedulable class itself (as demonstrated here), it appears to not perform the code contained in the @future method.

I know that we use Test.startTest() and Test.stopTest() in the test method to fire asynchronous code placed between them, but it seems that only causes the top-level asynchronous code to fire. Is there a way for me to cause all levels of asynch code to fire within a single test method?

3
  • I'm seeing the same issue. Mar 25, 2015 at 2:30
  • I'm also seeing this issue. Did you manage to solve it/work around it? If so, can you please update with an answer? Thanks!
    – mkorman
    Mar 30, 2015 at 12:55
  • @mkorman check my answer below, hope it helps!
    – smohyee
    Mar 30, 2015 at 19:02

1 Answer 1

8

Sorry for not answering this back when I figured it out. I don't remember exactly, but I believe it was due to a built in limit in testing only one 'level' of asynchronous call. The test environment will run an async call (like a batch, an @future method, or a scheduled class) within the Test.startTest() and Test.stopTest() framework, but it wont run any further asynch calls made within the first async call. In other words, you can't test async calls that have been chained together, at least not using a single test method.

The solution I ended up using was to simply write a different test method for each part - one to test that the scheduled class ran when scheduled, and a separate test for the @future method. I believe I may have also added an if(!Test.isRunningTest()) clause to the @future method call in my scheduled class to prevent issues during testing, though I don't remember if that was necessary.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .