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
Vivek NayakVivek Nayak 

Unable to cover Apex class which returns a wrapper in to Test class

Hi All,

I am trying to write test class for a class which makes HTTP callout  and returns response in wrapper. Below is my code

//Callout Class. This is a blurpint please avaoid syntax error

public class CalloutClass {

@AuraEnabled
public static wrapperClass CalloutMethod (string wslr){   //This method returna a wrapper

wrapperClass wrapper =new wrapperClass();

Http h=new Http(); 
HttpRequest req=new HttpRequest();
/*
Request Params
*/

HttpResponse res=h.send(req);

/*
Deserialize response
*/

if(res.getStatusCode()==200){
wrapper.Status=res.getStatus();
wrapper.StatusCode=res.getStatusCode();
wrapper.response=res.getBody();

}

return wrapper;
}


public class wrapperClass{

@AuraEnabled public string Status {get; set;}
@AuraEnabled public integer StatusCode {get; set;}
@AuraEnabled public list<object> response {get; set;}
}


}

//Mock Class

@isTest
public class MockCallout implements HTTPCalloutMock {

Public HTTPResponse respond(HTTPRequest Req) {
    HTTPResponse Res=New HTTPResponse();
    
    Res.setStatusCode(200); 
    Res.setStatus('OK');    
    //Res.setBody(); //callout response is list of objects so dont know how to set response here without line break 
    return Res;    
    }


}

//Test Class

@isTest
Public Class OP_TestAppointmentCallout {

@isTest 
public Static void TestAppointmentData(){


   Test.setMock(HttpCalloutMock.class,new MockCallout());   

 
    CalloutClass.wrapperClass Wrapper=CalloutClass.CalloutMethod('93411');
    
    String Actualstatus =Wrapper.Status;
    Integer Actualstatuscode =Wrapper.StatusCode;
    
    
    system.assertEquals('OK',Actualstatus);
    system.assertEquals(200,Actualstatuscode);
   
    }

}

When i run test i am getting Null pointer exception for test class. I am not sure how to set fake response in mock class and how to call actuall class in test class. Please help 
Durga PaavanDurga Paavan
Hi Vivek,

If you know the original response structure then create same JSON structure. Please find below link for your reference.

http://khaidoan.wikidot.com/salesforce-developer-apex-rest-callout

Hope above link gives answer to your issues!!!

Cheers,
Durgapaavan
Vivek NayakVivek Nayak
Thanks for reply. Closing it as it was some miss from my side.
Ritesh Chimankar 6Ritesh Chimankar 6
Hey help me I am in same kind of scenario, I am using test.setMock() to retrive dummy body of API call, but I have used a wrapper class same as yours where I can set two diffrent status such as 'APIERROR' and 'SUCCESSTOKEN', and I am using if(wrapper.status==APIERROR){ //CODE } in my apex class but I am unable to execute that if part in an test class. Please Help....