+ Start a Discussion
TerminusbotTerminusbot 

MockHttpResponse for @Future Callouts - Test Class

I'm reading through the documentation and the example provide shows the following for creating a test class using MockHttpResponse. 
@isTest
private class CalloutClassTest {
     @isTest static void testCallout() {
        // Set mock callout class 
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
        
        // Call method to test.
        // This causes a fake response to be sent
        // from the class that implements HttpCalloutMock. 
        HttpResponse res = CalloutClass.getInfoFromExternalService();
        
        // Verify response received contains fake values
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"foo":"bar"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
    }
}

Since my class is @future it does not return anything. How do I approach creating the test class for a callout that does not return anything. I've been reading and see the use of Test.startTest() and Test.stopTest() but not sure where to apply that in my test class. 

Thanks for the help. 
RaidanRaidan
You can use the startTest and stopTest when you make the call to the class method you are testing. In this case, line 10 of your code will look like below:
Test.startTest();
HttpResponse res = CalloutClass.getInfoFromExternalService();
Test.stopTest();

 
TerminusbotTerminusbot
Thanks Raidan for the response. I am still running into the issue that my CalloutClass does not return a value since it's void. Future classes can't return a value. So I am getting this error:
 
Illegal assignment from void to System.HttpResponse

That is due to the fact that HttpResponse res = CalloutClass.getInfoFromExternalService(); is epecting a returned value. 
RaidanRaidan
Ah! So your getInfoFromExternalService() is a future method. The only way I can think of is to make getInfoFromExternalService() method to return the HttpResponse object, and then you can call the method from a future method. Something like this:
public static HttpResponse getInfoFromExternalService() {
    HttpResponse response = res;
    //your code here
   return res;
}

@future
public static void futureMethod() {
    getInfoFromExternalService();
}
TerminusbotTerminusbot
Thanks. I will give that a try. Here is a hack I just tried to see if it works. 
 
@isTest
public class TestClientQueryCallout {



     @isTest static void testCallout() {
     
        // Set mock callout class 
        Test.setMock(HttpCalloutMock.class, new MockClientQueryResponse());
        
        // Call method to test.
        // This causes a fake response to be sent
        // from the class that implements HttpCalloutMock. 
        Test.startTest();
        ClientBatchQuery.sendBatchQuery();
        Test.stopTest();
        HttpRequest req = new HttpRequest();
        MockClientQueryResponse mock = new MockClientQueryResponse();
        HttpResponse res = mock.respond(req);
        // Verify response received contains fake values
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'text/xml');
        String actualValue = res.getBody();
        String expectedValue = '{"foo":"bar"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
    }
}