4

I'm currently playing around with SFDC-Http Integration and the new Mock server functionality.

In the Apex code which I am testing, I have this branch condition:

if (response.getStatusCode() != 200) { debugResponse (response); }
        else { update (processInvoiceList(invoiceIdList, response)); }  

... and I can thoroughly test what happens if the status code is 200.

However, if the status code is not 200, the code executed is simply this:

 /**
 * Write debugging information
 * @param HttpResponse response : Http response (e.g., from sendJsonToRemoteService())
 * @return None
 **/
private static void debugResponse (HttpResponse response)
{
    System.debug(                                                   
                LoggingLevel.ERROR , 
                '!!!!! Error from ' + request.getEndpoint() + ' : ' + response.getStatusCode() + ' ' + response.getStatus()
                ); 
}

This being the case, as the code currently stands, the only changes made to the system to check for would be to check the debug log.

I realize I could refactor my source to expose the HttpResponse (i.e. make it a public class variable), but aside from testing there is no need to expose it, so I don't want to if I don't need to.

So, instead, I'd like my unit test to parse the error log for the error message.

Is this possible?

If so, how?

1 Answer 1

5

No, you can't parse the log files. Maybe you could use a new feature available and annotate a private member variable with the @TestVisible annotation. This would then be visible to your tests but wouldn't be accessible when the code runs in production.

5
  • 2
    Alternatively, you could write a custom logging class used for testing purposes. The utility class for this could return early if not running a test.
    – sfdcfox
    Jul 19, 2013 at 16:33
  • Can @ TestVisible be used WITHIN functions (e.g. private static void example() { @TestVisible HttpResponse response = new HttpResponse(); ... } or would I need to make the HttpResponse visbile to the entire class? Jul 20, 2013 at 7:25
  • @sfdcfox, if I understand what you are saying correctly, I would need to modify my source code to test whether a test is running and, if so, send the debug information down a different path? Jul 20, 2013 at 7:29
  • That's probably how I would do it, for testing purposes. Something like Logger.logData(Logger.HTTPRESPONSEDATA, res.getStatus()), where the logger class has a ton of enums or static values for logging purposes. You could then query it through Logger.getLogsFor(Logger.HTTPRESPONSEDATA). I might draw up a formal code sample, if you're interested.
    – sfdcfox
    Jul 20, 2013 at 16:55
  • @sfdcfox, that indeed sounds interesting. Please share. :-) Jul 24, 2013 at 11:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .