0

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);
        Test.startTest();
        AccountLoader.retrieveAccountInfo();
        Test.stopTest();
        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.

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

1 Answer 1

0

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

2
  • even though I change the requestURI, I am seeing the same error
    – SFDCUser
    Sep 30, 2020 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. Oct 7, 2020 at 9:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .