The following api had been already implemented and I need to write test classes for it. From the examples I went through, I could see that the methods typically have a return object whereas the following has a void return type.

I don't know how to do the assertions.

global with sharing class ApplicationAPI {

    global static void get() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        try {
            String path = RestUtil.getPathParam(req, 'application/');
              //do something
              res.responseBody = setSomething;
        catch(BadRequestException e) {
            res.statusCode = e.getStatusCode();
            RestUtil.setResErrorBody(res, e.getErrorCode(), e.getMessage());

The test code I'm attempting:

    @isTest static void testGetApplications(){
        //init(); create records
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();

        req.requestURI = '/v1/application';  
        req.httpMethod = 'GET';  
        RestContext.request = req;
        RestContext.response = res;

        //what should be returned? or Asserted?

You would check res depending on what you expected back. Your "return value" just happens to be in a static variable, rather than being returned to a local variable.

// examples //
System.assertEquals(res.responseBody, expectedResponse);
System.assertEquals(res.statusCode, 200);

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.