How do we write test method for Apex class used by Lightning component where my return type of method is String but within the method I call rest api

Here is my class method:

public static String getResponse(String strURL, String method, String header){
    Http http = new Http();
    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    if (header != null) {
        req.setHeader('Content-Type', 'application/json');
    req.setBody = null;
    if (method == null) {req.setMethod = 'GET';}
    try {
        res = http.send(req); 
        if (res.getStatusCode() != 200) {
            throw new AuraHandledException(res.getStatus());
        String responseBody = res.getBody(); 
        return responseBody;
    catch (exception e) {
        throw new AuraHandledException(e.getMessage() + ' : ' + e.getStackTraceString());


This getResponse method is then called inside multiple methods within the class. For example:

public static String getInformation() {
    //other queries
    String passsURL = 'someURL';
    String response = getResponse(someURL, 'POST', '"Content-Type", "application/json"');
    return response;

I looked at how to write a test class for the API call described here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm

I created MockHttpResponseGenerator class as described here but not sure how will test class call method which returns string and makes an api call?

@isTest static void testCallout() {

    String url = 'http://example.com/example/test';

    // Set mock callout class
    Test.setMock(HttpCalloutMock.class, new DHS_Lab_MockHttpResponseGenerator());

    // Call method to test.
    // This causes a fake response to be sent
    // from the class that implements HttpCalloutMock.
    String response = MyClass.getResponse(url, null);

    // Verify response received contains fake values
    HttpResponse res = MyClass.getResponse(url, null, null);
    String contentType = res.getHeader('Content-Type');
    System.assert(contentType == 'application/json');
    String actualValue = res.getBody();
    String expectedValue = '{"example":"test"}';
    System.assertEquals(actualValue, expectedValue);
    System.assertEquals(200, res.getStatusCode());

How do I verify test response and at the same time add code coverage for the above class? Assert statements are required for me to meet our internal standards. Please help


Testing the return values of your mock is pointless. That's not testing the class, that's testing the mock. Make a separate unit test for that if you must, but the main point of this test is to test your class. When you give it params, what do you get back out? A string.

That string should match an expected value. I'm not sure what that means, because I don't know what your mock returns, or perhaps it doesn't even matter... the assertion could simply be that the value wasn't null. You also need two extra mock versions, one for testing each of the two AuraHandledExceptions and get 100% coverage.

That would look like:

 try {
   String response = MyClass.getResponse(url, null);
   System.assert(false, 'Expected Exception');
 } catch(AuraHandledException e) {
   System.assertEquals('some expected error message',e.getMessage());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.