You need to sign in to do that
Don't have an account?
Missing something on the Animal Locator exercise
I don't know what I am doing wrong. I've been working on the Animal Locator exercise in trailhead having to do with REST API Callouts and just can not get the result I'm working on to pass the 'Challenge'. My code is 100% tested, and it seems to meet all the criteria, but I keep getting the following message: "Challenge Not yet complete... here's what's wrong:
Executing the 'getAnimalNameById' method on 'AnimalLocator' failed. Make sure the method exists with the name 'getAnimalNameById', is public and static, accepts an Integer and returns a String."
Below is my code:
AnimalLocator.apxc (Class)
public class AnimalLocator {
public static string getAnimalNameById(integer numSubmitted) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + numSubmitted);
request.setMethod('GET');
HttpResponse response = http.send(request);
string replyName = 'None returned';
if (response.getStatusCode() == 200) {
replyName = response.getBody();
}
return replyName;
}
}
AnimalLocatorTest.apxc
@IsTest
private class AnimalLocatorTest {
@isTest static void testGetCallout() {
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
// This causes a fake response to be sent
// from the class that implements HttpCalloutMock.
String animalname = AnimalLocator.getAnimalNameById(2);
// Verify that the response received contains fake values
String expectedValue = 'Charles H Bones Esquire';
System.assertEquals(animalname, expectedValue);
}
}
AnimalLocatorMock.apxc
@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('Charles H Bones Esquire');
response.setStatusCode(200);
return response;
}
}
My code is saved and closed out of and the challenge still fails. Any suggestions would be greatly appreciated. Thanks!
Executing the 'getAnimalNameById' method on 'AnimalLocator' failed. Make sure the method exists with the name 'getAnimalNameById', is public and static, accepts an Integer and returns a String."
Below is my code:
AnimalLocator.apxc (Class)
public class AnimalLocator {
public static string getAnimalNameById(integer numSubmitted) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + numSubmitted);
request.setMethod('GET');
HttpResponse response = http.send(request);
string replyName = 'None returned';
if (response.getStatusCode() == 200) {
replyName = response.getBody();
}
return replyName;
}
}
AnimalLocatorTest.apxc
@IsTest
private class AnimalLocatorTest {
@isTest static void testGetCallout() {
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
// This causes a fake response to be sent
// from the class that implements HttpCalloutMock.
String animalname = AnimalLocator.getAnimalNameById(2);
// Verify that the response received contains fake values
String expectedValue = 'Charles H Bones Esquire';
System.assertEquals(animalname, expectedValue);
}
}
AnimalLocatorMock.apxc
@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('Charles H Bones Esquire');
response.setStatusCode(200);
return response;
}
}
My code is saved and closed out of and the challenge still fails. Any suggestions would be greatly appreciated. Thanks!
Alap Mistry
All Answers
Alap Mistry
Ya, unfortunately, I added that remote site setting back when I first started struggling with this exercise. Check out the screen shot that I have attached.
I welcome any other suggestions that might be out there!
Thank you!
Eric Anderson
Alap Mistry
As you identified, I missed the fact that I needed to use the 'Map' functions in order to parse the response. Additionally, I needed to provide all of the various attributes for the response, not just the Name.
Thank you again!
- Eric -
SETUP =>Remote Site Settings => New Remote Site => https://th-apex-http-callout.herokuapp.com