Activity Forums Salesforce® Discussions How to test future methods in test class in Salesforce?

  • MOHIT

    Member
    June 2, 2020 at 2:13 pm

    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);
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos