Test class for an array of objects as response

PHOTO EMBED

Fri May 12 2023 15:53:44 GMT+0000 (Coordinated Universal Time)

Saved by @gbritgs #apex

@isTest
    static void getTest(){
        final String RESPONSE_TOKEN = '{ "access_token": "xxxxxxxxx", "token_type": "Bearer"}';
        final String REQUEST_BODY = '['+
            '    {'+
            '        "code": "329",'+
            '        "nameSummarized": "Test"'+
            '    }'+
            ']';
        
        TestUtils.Mock mock = TestUtils.setMultipleMocks(RESPONSE_TOKEN, 200);
        mock = TestUtils.setMultipleMocks(REQUEST_BODY, 200, mock);
        
        Boolean catchException = false;
        String requestBody = REQUEST_BODY;
        List<sObject> responseBody = null;
        
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, mock);
        
        try {
            responseBody = (List<sObject>) JSON.deserialize(requestBody, List<sObject>.class);
            //call method to be tested
        } catch(Exception e){
            catchException = true;
        }
        
        Test.stopTest();
        
        System.assertEquals(false, catchException);
    }
content_copyCOPY

Example of mock of an array of objects as response