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

 

 

Ron HessRon Hess

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

Ken_KoellnerKen_Koellner

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.

 

 

TehNrdTehNrd

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

md1md1
Just voted the idea! But I'm stuck with the code coverage not going up as I'm handling the over 500 records in the code and it fails for it!
Marc C.Marc C.

The correct syntax is (now):

 

Test.startTest();

 

Test.stopTest();

Bryce CBryce C
I had a similar issue and the above didn't work. What did work was this: create a non-static method that the future method calls by instantiating the class. Then I can call the non-static method during tests and the future method during triggers (where async operations aren't allowed outside of static future methods).
So something like:
public class AClass {

    @future // required since this is an async call-out.
    public static void afunction_future(){ (new ClassName).afunction(); }

    public void afunction(){
        // do stuff here, it'll be called by the static function.
    }

}
Then in your trigger:
AClass.afunction_future(); // this is how you call a static method.
And in your test:
(new AClass()).afunction(); // non-static method requries an instance of the class.
Your test won't mind that you are calling an async operation in the regular flow, but the trigger will.
Bruce Van Buren 8Bruce Van Buren 8
Same issue:   
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.  

@future // required since this is an async call-out.
public static void afunction_future(){ 
     afunction(); 
   }

 public static void afunction(){
        // do stuff here, it'll be called by the static function.
    }

 
farukh sk hdfarukh sk hd
To test future method,we need to call future method between test.startTest() and test.stopTest().
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
NipuWNipuW
If this is callout method (@future(callout=true) then you might need to create a mock callout to test the method.