
public class AnimalLocator {
public static String getAnimalNameById(Integer ID) {
String animal = ' ';
Http http = new Http();
HttpRequest request = new HttpRequest();
String s = string.valueOf(ID);
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+ ID);
request.setMethod('GET');
HttpResponse response = http.send(request);
Map<String,Object> animals = new Map<String,Object>();
if(response.getStatusCode() == 200) {
Map<String,Object> results = (Map<String,Object>)JSON.deserializeUntyped(response.getBody());
animals = (Map<String,Object>) results.get('animal');
animal = String.valueOf(animals.get('name'));
} else {
system.debug(response.getBody());
}
Return animal;
}
}
AnimalLocatorMock Class:
@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
// Implement this interface method
global HTTPResponse respond(HTTPRequest request) {
// Create a fake response
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setBody('{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');
response.setStatusCode(200);
return response;
}
}
AnimalLocatorTest Class:
@isTest
private class AnimalLocatorTest {
@isTest
static void testGetCallout() {
Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
String animalName = AnimalLocator.getAnimalNameById(1);
system.debug('AnimalName : ' + animalName);
System.assertEquals(animalName, 'chicken');
}
}
Hi,Is namespace or domain is enable in your org ?Please check below post for same issue1) https://developer.salesforce.com/forums/?id=906F0000000MJiPIAW2) https://developer.salesforce.com/forums/?id=906F0000000MJiKIAWLet us know if this will help you