Below is my test method:

static testMethod void callbackdateTomorrow() {
        Date currentDate;
        CurrentDate = date.today();

        System.RestContext.request = new RestRequest();
        System.RestContext.response = new RestResponse();
        RestContext.request.requestURI = 'https://test.salesforce.com/';
        RestContext.request.httpMethod = 'POST';
        Account_Codes__c custAcc = new Account_Codes__c;
        custAcc.First_name__c = 'Test';
        custAcc.Last_Name__c = 'Account';
        custAcc.Call_Back_Date__c = DateTime.now().addHours(-1).addDays(1);
        insert custAcc;
        String jsonStr = '{"Key": "Id","sfids": [';
        jsonStr += '{"id":"' + custAcc.Id + '"}]}';

        RestContext.request.requestBody = blob.valueof(jsonStr);
        String response = RestContext.response.responseBody.toString();
        List<Object> responseObj = (List<Object>)JSON.deserializeUntyped(response);
        System.assert(responseObj.size() == 1, 'Accounts should be excluded from system');
        Map<String, Object> custAccDes = (Map<String, Object>)responseObj[0];

Now when i run this test class, I'm getting the error as

System.AssertException: Assertion Failed: Accounts should be excluded from system

Also, I am getting error from the line Map<String, Object> custAccDes = (Map<String, Object>)responseObj[0]; which is

System.ListException: List index out of bounds: 0

when i checked the debug log, the JsonStr is getting converted to blob value without any issue. Below is my jsonStr output

{"Key": "Id","sfids": [{"id":"a000P99877048kasqOM"}]}

I'm wondering where did it went wrong.

Can anyone please suggest any changes in the above code so that I can get these errors solved.

  • 1
    What is String response = RestContext.response.responseBody.toString(); , what is the response here? Can you print System.debug(response); – Nagendra Singh Sep 30 '20 at 8:53
  • @NagendraSingh For that line String reponse, I am getting the error as "System.NullPointerException: Argument cannot be null." – SFDCUser Sep 30 '20 at 9:02

The requestURI used '*https://test.salesforce.com/*' in the RestContext is incorrect. It should point to '/services/apexrest/{URLMapping}'.

  • even though I change the requestURI, I am seeing the same error – SFDCUser Sep 30 '20 at 9:26
  • If you are getting an error even after updating the requestURI with a correct value then there is another issue probably in AccountLoader.retrieveAccountInfo(). It must be returning null hence the Null Pointer and List index out of bounds: 0 errors. – TrafalgarLaw Oct 7 '20 at 9:30

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.