You need to sign in to do that
Don't have an account?
Ken_Koellner
how to test @future methods???
I'm trying to test an Apex method that calls an @future method. I looks like my testing works up until the point where the @future test is called. The test checks some results from the @future method and it looks like they haven't been executed yet.
Does @future execute asyncronously in test mode?
If so, how does one test @future methods?
Hi Ken ,
yes there is a method to do this.
you must use System.startTest() and system.stopTest().
Simply call your @future method in-between these, and then make your asserts after the stopTest()
The stopTest() will cause the @future method to fire, and the next statement can verify the functionality you developed in your future method
I thought that would help but it doesn't really.
The reason I'm using @future is that I have to update > 100 rows in OpportunityLineItem via a trigger on Opportunity. When I used the stopTest(), I hit the 100 DML limit in a trigger. Normally, @future works around that limit. It sounds like stopTest() isn't doing things quite right.
Hey Ken,
You are right. Please vote for this idea:
http://ideas.salesforce.com/article/show/10097033/future_asynchrounous_unit_test_must_reset_governor_limits
The correct syntax is (now):
Test.startTest();
Test.stopTest();
So something like: Then in your trigger: And in your test: Your test won't mind that you are calling an async operation in the regular flow, but the trigger will.
This is how i handel the same using bryce's example. Both stratigies work but 0 test coverage on the actual future method. Always make it a single line call from the future method to the local method.
You end up with test.isTestRunning() The upside it is easier to test and you can run anonymous as well.
When test.startTest() runs data is collected asynchronously and as soon as test.stopTest runs data collected is
run synchronously.
test.startTest();
myClass.futureMethodName(id);
test.stopTest();
For more information visit,
https://www.sfdc-lightning.com/2018/10/future-methods-in-salesforce.html