function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sai_lavusai_lavu 

@future annotations and unit testing

Hi,

I have the following method in a class called Project, which summarizes Opportunities linked to the custom Project object.

Code:
    @future 
    public static void updateSummaryFromOpportunities(List<Id> projectIds) {  
        if(projectIds == null || projectIds.size() == 0)
            throw new InvalidParameterException('No Projects provided.'); 
        ...
    }

 
The unit test below is failing. If I remove the @future annotation the test passes. Is this expected behaviour or is it a bug?

Code:
    static testMethod void testUpdateSummaryFromOpportunities_CheckParam() {
try {
Project.updateSummaryFromOpportunities(null);
} catch(InvalidParameterException e) {
System.assertEquals('No Projects provided.', e.getMessage());
}
}

 

cheenathcheenath
This is the expected behavior. Your future method is throwing an exception, which is not caught by anyone.
When you are calling this method in sync, you are catching the exception and asserting.

Future methods are called latter from a different thread and it is not a good idea to throw exception from them.