Saturday, April 25, 2015

How to write unit test classes for @future methods

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. 

0 comments:

Post a Comment