Activity Forums Salesforce® Discussions How to write test Class for Future Methods?

  • Manpreet

    Member
    April 27, 2017 at 1:43 pm

    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!

  • shariq

    Member
    September 18, 2018 at 6:58 am

    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.

Popular Salesforce Blogs

Popular Salesforce Videos