Activity › Forums › Salesforce® Discussions › How to write test Class for Future Methods?
-
How to write test Class for Future Methods?
Posted by Suraj on April 27, 2017 at 1:33 PMHow to write test Class for Future Methods?
shariq replied 7 years, 9 months ago 3 Members · 2 Replies -
2 Replies
-
Hi Suraj,
Put the call to the future method inside startTest/stopTest:
Test.startTest();myClass.futuremethod( someID );
Test.stopTest();
Test.stopTest() does not return until your future method has completed.Thanks!
- [adinserter block='9']
-
Hi,
To test methods defined with the future annotation, call the class containing the method in a startTest(), stopTest()code block. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously.
@isTest
private class FutureApexTest {private static testMethod void callFutureMethod() {
Test.startTest();
// Call your @future method here.
FutureApex.doCallFuture();// Now that we’ve queued up the @future method to be executed, we
// need to call Test.stopTest(). When Test.stopTest() runs, all
// queued and scheduled Apex that would occur in the future is actually
// executed immediately at this time.Test.stopTest();
// Validate the expected results.
System.assertEquals(expectedValue, actualValue);}
}Hope this helps!
Log In to reply.