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.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?
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.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.