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
degmodegmo 

Test coverage for a future method

Hello All,
I have an existing future method that does a callout to an external service.   All it does is send data to an external service and if the response is not a 200, it just logs a message to the debug log.  This method obviously doesn't return anything and it doesn't perform any action on the SF side that I can use to do my assertions.

I have created a mock class and used it to invoke this future method inside of Test.StartTest & Test.StopTest.  I am getting 100% code coverage but I don't have any assertions.  I am struggling with what to use for my assertions?  Since the method doesn't return anything, I can't use the response from my mock class.  Any thoughts?  Here is a code snippet of the class  
@Future(Callout = true)
public static void callExternalSystem(Id recordId) {

     String body = buildMessage(recordId);
     HttpRequest req = new HttpRequest();
     req.setEndpoint('https://xyz.com/test');
     req.setMethod('Post');
     req.setHeader('');
     req.setBody(body);
     Http http = new Http();
     HttpResponse response = http.send(req);

     if(response.getStatusCode() != 200) {
          system.debug('Status is: ' + response.getStatus + '  ' + response.getBody());
     }

}

public static string buildMessage(Id recordId)
{
  // logic to build the request body
}

 
RituSharmaRituSharma
You may assert the status code.
AbhishekAbhishek (Salesforce Developers) 
Please note that if the apex code and the test class have 100% code coverage and you are still unable to deploy it in production due to low code coverage. It means that the overall code coverage of other existing components (trigger and classes) in production is below 75% and the deployment will not be successful.

For code snippet, you can go through the below developer discussion,

https://salesforce.stackexchange.com/questions/65653/how-to-test-a-future-method-which-makes-callouts

https://developer.salesforce.com/forums/?id=906F000000094QOIAY


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
degmodegmo
@Ritu, how can assert the status code when the future method doesn't return anything?