0

I have a RestResource mapped to /Demo/ that I place on a Salesforce Site. The rest resource works great, including the parameters. However, in my test class I cannot get it to return anything to RestContext.response.responseBody

My first idea was that maybe it was having difficulty connecting to the endpoint, so I tried different combinations of req.requestURI (including a full URL) but it always returns the same.

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

@HttpGet
global static List<Good__c> getRecords(){

    String location = RestContext.request.params.get('location');
    String id = RestContext.request.params.get('id');

    String qryString = 'Select Id,Name,Location__c from Good__c WHERE Id !=NULL ';

    List<Good__c> DemoGoods;

    if (!String.isBlank(location)){qryString += ' AND Location__c =: location ';}
    if (!String.isBlank(id)){qryString += ' AND id =: id ';}

    DemoGoods = Database.query(qryString);

    return DemoGoods;

}
}

Test Class

In this Test Class, I plan on adding System Asserts to test whether the response is 200, among other tests. However, I need to figure out why RestContext.response.responseBody is returning NULL.

@isTest
private class DemoControllerTest 
{

@isTest 
static void TestBehavior()
    {
        RestRequest req   = new RestRequest();
        RestResponse res = new RestResponse();

        System.Debug('About to Call Request');

        req.requestURI    = '/services/apexrest/Demo';
        req.httpMethod    = 'GET';

        RestContext.request   = req;
        RestContext.response  = res; 

        DemoController.getRecords();

        System.Debug('Response Body ---' + RestContext.response.responseBody);

    }
}

Debug Only

09:18:47:005 USER_DEBUG [11]|DEBUG|About to Call Request
09:18:47:032 USER_DEBUG [21]|DEBUG|Response Body ---null

Here is my full execution log:

46.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;NBA,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
06:18:47.0 (729496)|EXECUTION_STARTED
06:18:47.0 (756392)|CODE_UNIT_STARTED|[EXTERNAL]|01p0a000004xIgq|DemoControllerTest.TestBehavior()
06:18:47.0 (1295960)|HEAP_ALLOCATE|[72]|Bytes:3
06:18:47.0 (1364154)|HEAP_ALLOCATE|[77]|Bytes:152
06:18:47.0 (1394728)|HEAP_ALLOCATE|[342]|Bytes:408
06:18:47.0 (1422420)|HEAP_ALLOCATE|[355]|Bytes:408
06:18:47.0 (1446941)|HEAP_ALLOCATE|[467]|Bytes:48
06:18:47.0 (1490047)|HEAP_ALLOCATE|[139]|Bytes:6
06:18:47.0 (1518890)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:24
06:18:47.0 (1548123)|METHOD_ENTRY|[2]|01p0a000004xIgq|DemoControllerTest.DemoControllerTest()
06:18:47.0 (1563702)|STATEMENT_EXECUTE|[2]
06:18:47.0 (1577783)|STATEMENT_EXECUTE|[2]
06:18:47.0 (1595158)|METHOD_EXIT|[2]|DemoControllerTest
06:18:47.0 (1652324)|HEAP_ALLOCATE|[50]|Bytes:5
06:18:47.0 (1693441)|HEAP_ALLOCATE|[56]|Bytes:5
06:18:47.0 (1723667)|HEAP_ALLOCATE|[64]|Bytes:7
06:18:47.0 (1803840)|STATEMENT_EXECUTE|[7]
06:18:47.0 (1811414)|STATEMENT_EXECUTE|[8]
06:18:47.0 (1870147)|HEAP_ALLOCATE|[8]|Bytes:252
06:18:47.0 (1884929)|HEAP_ALLOCATE|[8]|Bytes:25
06:18:47.0 (1905744)|SYSTEM_METHOD_ENTRY|[1]|RestRequest.RestRequest()
06:18:47.0 (1916143)|STATEMENT_EXECUTE|[1]
06:18:47.0 (1932189)|SYSTEM_METHOD_EXIT|[1]|RestRequest
06:18:47.0 (1946797)|HEAP_ALLOCATE|[8]|Bytes:32
06:18:47.0 (4315658)|VARIABLE_SCOPE_BEGIN|[8]|req|System.RestRequest|true|false
06:18:47.0 (4603166)|VARIABLE_ASSIGNMENT|[8]|req|{"headers":"0x42cc5b85","params":"0xed23ef"}|0x4e21fd8d
06:18:47.0 (4626102)|STATEMENT_EXECUTE|[9]
06:18:47.0 (4691893)|HEAP_ALLOCATE|[9]|Bytes:156
06:18:47.0 (4709733)|HEAP_ALLOCATE|[9]|Bytes:10
06:18:47.0 (4732626)|SYSTEM_METHOD_ENTRY|[1]|RestResponse.RestResponse()
06:18:47.0 (4744081)|STATEMENT_EXECUTE|[1]
06:18:47.0 (4760739)|SYSTEM_METHOD_EXIT|[1]|RestResponse
06:18:47.0 (4777072)|HEAP_ALLOCATE|[9]|Bytes:16
06:18:47.0 (5000780)|VARIABLE_SCOPE_BEGIN|[9]|res|System.RestResponse|true|false
06:18:47.0 (5077676)|VARIABLE_ASSIGNMENT|[9]|res|{"headers":"0x16dd0c0a"}|0x7f46a765
06:18:47.0 (5096376)|STATEMENT_EXECUTE|[11]
06:18:47.0 (5105111)|HEAP_ALLOCATE|[11]|Bytes:21
06:18:47.0 (5158443)|USER_DEBUG|[11]|DEBUG|About to Call Request
06:18:47.0 (5182146)|STATEMENT_EXECUTE|[13]
06:18:47.0 (5191476)|HEAP_ALLOCATE|[13]|Bytes:23
06:18:47.0 (5260447)|STATEMENT_EXECUTE|[14]
06:18:47.0 (5270525)|HEAP_ALLOCATE|[14]|Bytes:3
06:18:47.0 (5327604)|STATEMENT_EXECUTE|[16]
06:18:47.0 (5421786)|VARIABLE_SCOPE_BEGIN|[3]|System.RestContext.request|System.RestRequest|true|true
06:18:47.0 (5454040)|VARIABLE_SCOPE_BEGIN|[4]|System.RestContext.response|System.RestResponse|true|true
06:18:47.0 (5475672)|HEAP_ALLOCATE|[16]|Bytes:112
06:18:47.0 (5487565)|HEAP_ALLOCATE|[16]|Bytes:5
06:18:47.0 (5506776)|SYSTEM_METHOD_ENTRY|[1]|RestContext.RestContext()
06:18:47.0 (5517116)|STATEMENT_EXECUTE|[1]
06:18:47.0 (5536021)|SYSTEM_METHOD_EXIT|[1]|RestContext
06:18:47.0 (5696520)|STATEMENT_EXECUTE|[17]
06:18:47.0 (5775005)|STATEMENT_EXECUTE|[19]
06:18:47.0 (10858926)|HEAP_ALLOCATE|[19]|Bytes:1
06:18:47.0 (10914617)|METHOD_ENTRY|[2]|01p0a000004xIGS|DemoController.DemoController()
06:18:47.0 (10938605)|STATEMENT_EXECUTE|[2]
06:18:47.0 (10973654)|STATEMENT_EXECUTE|[2]
06:18:47.0 (11034287)|METHOD_EXIT|[2]|DemoController
06:18:47.0 (11086149)|METHOD_ENTRY|[19]|01p0a000004xIGS|DemoController.getRecords()
06:18:47.0 (11269234)|STATEMENT_EXECUTE|[5]
06:18:47.0 (11279588)|STATEMENT_EXECUTE|[7]
06:18:47.0 (11393029)|HEAP_ALLOCATE|[7]|Bytes:8
06:18:47.0 (11441704)|VARIABLE_SCOPE_BEGIN|[7]|location|String|false|false
06:18:47.0 (11481649)|VARIABLE_ASSIGNMENT|[7]|location|null
06:18:47.0 (11497412)|STATEMENT_EXECUTE|[8]
06:18:47.0 (11561030)|HEAP_ALLOCATE|[8]|Bytes:2
06:18:47.0 (11595516)|VARIABLE_SCOPE_BEGIN|[8]|id|String|false|false
06:18:47.0 (11619080)|VARIABLE_ASSIGNMENT|[8]|id|null
06:18:47.0 (11630485)|STATEMENT_EXECUTE|[10]
06:18:47.0 (11639122)|HEAP_ALLOCATE|[10]|Bytes:56
06:18:47.0 (11658680)|VARIABLE_SCOPE_BEGIN|[10]|qryString|String|false|false
06:18:47.0 (11686710)|VARIABLE_ASSIGNMENT|[10]|qryString|"Select Id,Name,Locat (36 more) ..."
06:18:47.0 (11699375)|STATEMENT_EXECUTE|[12]
06:18:47.0 (11708945)|VARIABLE_SCOPE_BEGIN|[12]|DemoGoods|List<Good__c>|true|false
06:18:47.0 (11731440)|VARIABLE_ASSIGNMENT|[12]|DemoGoods|null|
06:18:47.0 (11836472)|STATEMENT_EXECUTE|[14]
06:18:47.0 (11889562)|STATEMENT_EXECUTE|[15]
06:18:47.0 (11897586)|STATEMENT_EXECUTE|[17]
06:18:47.0 (25045909)|SOQL_EXECUTE_BEGIN|[17]|Aggregations:0|Select Id,Name,Location__c from Good__c WHERE Id !=NULL 
06:18:47.0 (32135089)|SOQL_EXECUTE_END|[17]|Rows:0
06:18:47.0 (32188386)|HEAP_ALLOCATE|[17]|Bytes:4
06:18:47.0 (32217032)|HEAP_ALLOCATE|[17]|Bytes:0
06:18:47.0 (32266902)|HEAP_ALLOCATE|[17]|Bytes:4
06:18:47.0 (32360676)|VARIABLE_ASSIGNMENT|[17]|DemoGoods|[]|0x1b8941c1
06:18:47.0 (32385071)|STATEMENT_EXECUTE|[19]
06:18:47.0 (32422584)|METHOD_EXIT|[19]|01p0a000004xIGS|DemoController.getRecords()
06:18:47.0 (32442263)|STATEMENT_EXECUTE|[21]
06:18:47.0 (32452492)|HEAP_ALLOCATE|[21]|Bytes:17
06:18:47.0 (32662907)|HEAP_ALLOCATE|[21]|Bytes:21
06:18:47.0 (32701869)|USER_DEBUG|[21]|DEBUG|Response Body ---null
06:18:47.32 (32756002)|CUMULATIVE_LIMIT_USAGE
06:18:47.32 (32756002)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

06:18:47.32 (32756002)|CUMULATIVE_LIMIT_USAGE_END

06:18:47.0 (33831491)|CODE_UNIT_FINISHED|DemoControllerTest.TestBehavior()
06:18:47.0 (35213626)|EXECUTION_FINISHED

I welcome any recommendations. Thank you.

  • RestContext.response is not automatically deserialized in unit test. It might be necessary to collect returned List<Good__c> from getRecords method and assert that – kurunve Aug 27 '19 at 13:29
1

You declare demoGoods locally, then you assign it and return it. But you never touch the RestContext.response in order to set it there. Your test expects you to do so.

DemoGoods = Database.query(qryString);
// here you need to set RestContext.response.responseBody
return DemoGoods;

Most likely, you will just serialize your records.

RestContext.response.responseBody = JSON.serialize(demoGoods);
| improve this answer | |

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.