How to write unit test classes for @future methods

by Sudipta Deb | Apr 25, 2015 | Apex, Salesforce, unit testing | 0 comments

In this post, I will explain how to write unit test classes to increase test coverage for @future methods. Let’s start with a Use Case –
Use Case –
Let’s say in my Account object, I have a checkbox named “notifyAdmin__c” and I would like to update this checkbox for list of Accounts in a future method.
Below is the Apex Class –

Now let’s write the test class. Below is the test class –

Here is the explanation-
To test future methods, you need to simply make your future method call between Test.startTest() and Test.stopTest(). Test.stopTest() will make sure that @future method will have fired. So after Test.stopTest(), you can do all your assert.

I hope this quick example will help you to write unit test cases for your @future method. If you have any suggestion, please let me know. 

Written by

Related Posts

Salesforce Cancels the “Permissions in Profiles” Retirement: What It Means for Admins and Architects

Salesforce Cancels the “Permissions in Profiles” Retirement: What It Means for Admins and Architects

For the last few years, Salesforce administrators, architects, and security teams have been preparing for a major platform change: the retirement of permissions managed directly through Profiles. Salesforce had previously announced plans to move organizations toward a Permission Set–led security model, with enforcement expected to begin around the Spring ’26 timeframe.

read more...

0 Comments

Leave a Reply