+ 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 {

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



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

public class MockCallout implements HTTPCalloutMock {

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


//Test Class

Public Class OP_TestAppointmentCallout {

public Static void TestAppointmentData(){

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

    CalloutClass.wrapperClass Wrapper=CalloutClass.CalloutMethod('93411');
    String Actualstatus =Wrapper.Status;
    Integer Actualstatuscode =Wrapper.StatusCode;


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.


Hope above link gives answer to your issues!!!

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....