2

This question already has an answer here:

I created a @HttpPost API end point called SampleAPI that doesn't return an object. Instead I am populating RestResponse like so:

@RestResource(urlMapping='/SampleAPI/*')
global class SampleAPI{

    @HttpPost
    global static void verifyData() {

        RestResponse response = RestContext.response;
        response.statusCode = 200;
        response.responseBody = Blob.valueOf('response body');
    }
}

In my test class I add the request body like so:

@IsTest
static void TestBehavior() {
    RestRequest request = new RestRequest();
    RestResponse response = new RestResponse();
    request.requestURI = '/services/apexrest/SampleAPI';
    request.httpMethod = 'POST';
    request.requestBody = Blob.valueof('test body');


    RestContext.request = request;
    RestContext.response = response;


    SampleAPI api = new SampleAPI();
}

How can I get the response body in my test method? I tried RestContext.response but that comes back as [headers={}, responseBody=null, statusCode=null] when I am actually expecting the statusCode and responseBody to not be null.

marked as duplicate by Mark Pond, Boris Bachovski, Oleksandr Berehovskyi, Raul, Vigneshwaran G Mar 14 '18 at 13:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Looks like something may have gotten trimmed out of your posted code: response = RestCoresponse.statusCode = 200; was this supposed to be two separate lines? – Mark Pond Mar 12 '18 at 22:25
  • @MarkPond Thank you. I think I fixed that. I can post more code if it's not clear enough. My problem is that in every article I read online the HttpPost method is returning a wrapper class. But I am curious how can I use the RestResponse body instead of returning a wrapper. – Arthlete Mar 12 '18 at 22:29
  • @MarkPond In the thread you linked the @HttpGet method is returning a string. In my case my method is not returning but instead is setting the responseBody on the RestResponse. When I hit the end point I get the responseBody but I cannot figure out how to get it in my test class. – Arthlete Mar 12 '18 at 22:38
  • @MarkPond I also changed my code to use the addParameter. Figured that one was wrong. But still the RestResponse is all nulls. – Arthlete Mar 12 '18 at 22:41
6

You need to call the actual http method for it to invoke the API and then if you check, it should have the response.

@IsTest
static void TestBehavior() {
    RestRequest request = new RestRequest();
    RestResponse response = new RestResponse();
    request.requestURI = '/services/apexrest/SampleAPI';
    request.httpMethod = 'POST';
    request.addParameter('test', 'test');

    RestContext.request = request;
    RestContext.response = response;

    SampleAPI.verifyData();

    //Now RestContext.response should have data returned from api call
}
  • 2
    Beat me to it by 2 minutes. Nice work! – Mark Pond Mar 12 '18 at 22:46

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