-
Notifications
You must be signed in to change notification settings - Fork 18
/
MockCalloutTestClass.cls
26 lines (24 loc) · 1.29 KB
/
MockCalloutTestClass.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@isTest
private class AnimalsCalloutTest {
@isTest static void testFetCallout() {
// Create the mock response based on a static resource
StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
mock.setStaticResource('GetAnimalResource');
mock.setStatusCode(200);
mock.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Associate the callout with a mock response
Test.setMock(HttpCalloutMock.class, mock);
// Call method to test
HttpResponse result = AnimalsCallouts.makeGetCallout();
// Verify mock response is not null
System.assertNotEquals(null, result, 'The callout returned a null response.');
// Verify status code
System.assertEquals(200, result.getStatusCode(), 'The status code is not 200.');
// Verify content type
System.assertEquals('application/json;charset=UTF-8',result.getHeader('Content-Type'),'The content type value is not expected.');
// Verify the array contains 3 items
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(result.getBody());
List<Object> animals = (List<Object>) results.get('animals');
System.assertEquals(3,animals.size(),'The array should only contain 3 items.');
}
}