You need to sign in to do that
Don't have an account?

How do I unit test a @future method that makes a callout?
I have a trigger that fires when an opportunity is updated, as part of that I need to call our API with some detail from the opportunity.
As per many suggestions on the web I've created a class that contains a @future method to make the callout.
I'm trying to catch an exception that gets thrown in the @future method, but the test method isn't seeing it.
The class under test looks like this:
here's the unit test:
As per many suggestions on the web I've created a class that contains a @future method to make the callout.
I'm trying to catch an exception that gets thrown in the @future method, but the test method isn't seeing it.
The class under test looks like this:
public with sharing class WDAPIInterface { public WDAPIInterface() { } @future(callout=true) public static void send(String endpoint, String method, String body) { HttpRequest req = new HttpRequest(); req.setEndpoint(endpoint); req.setMethod(method); req.setBody(body); Http http = new Http(); HttpResponse response = http.send(req); if(response.getStatusCode() != 201) { System.debug('Unexpected response from web service, expecte response status status 201 but got ' + response.getStatusCode()); throw new WDAPIException('Unexpected response from web service, expecte response status status 201 but got ' + response.getStatusCode()); } } }
here's the unit test:
@isTest static void test_exception_is_thrown_on_unexpected_response() { try { WDHttpCalloutMock mockResponse = new WDHttpCalloutMock(500, 'Complete', '', null); WDAPIInterface.send('https://example.com', 'POST', '{}'); } catch (WDAPIException ex) { return; } System.assert(false, 'Expected WDAPIException to be thrown, but it wasnt'); }Now, I've read that the way to test @future methods is to surround the call with Test.startTest() & Test.endTest(), however when I do that I get another error:
METHOD RESULT test_exception_is_thrown_on_unexpected_response : Skip MESSAGE Methods defined as TestMethod do not support Web service callouts, test skippedSo the question is, how do I unit test a @future method that makes an callout?
Try something like
So I tried it the way you are suggesting and I still get the same error:
This was posted a long time ago. But the solution is to place your @future method with the Test.startTest and Test.stopTest code blocks and place your try catch around Test.startTest and Test.stopTest.
The exception is bubbled up.