Skip to main content
Hi All,

Attempting the trailhead challenge for Apex REST Callouts. The class works and the test code also passes with 100% code coverage. However, trailhead is unable to find the AnimalLocatorTest class. Request please help.

AnimalLocator Class:

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');

}

}

 
2 answers
0/9000