You need to sign in to do that
Don't have an account?
Jefferson Fernandez
Trailhead: Apex REST Callout - Create an Apex class that calls a REST endpoint and write a test class
Hi All,
May I ask some help because of this error
"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."
All classes were saved without errors. Here are my codes:
<---AnimalLocator class--->
public class AnimalLocator {
public static String getAnimalNameById(Integer id) {
Http http = new Http();
HttpRequest request = new HTTPRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/:id');
request.setMethod('GET');
HttpResponse response = http.send(request);
String strRes = '';
System.debug('response code: ' + response.getStatusCode());
System.debug('reseponse body: ' + response.getBody());
//If the response is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
Map<String, Object> animals = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
//Cast the value in the animals' key as list.
// List<Object> animals = (List<Object>) results.get('animal');
System.debug('received the following: ' + animals);
strRes = String.valueOf(animals.get('name'));
System.debug('strRes: ' + strRes);
} // end if
return strRes;
} // end method
} // end classpublic class AnimalLocator {
public static String getAnimalNameById(Integer id) {
Http http = new Http();
HttpRequest request = new HTTPRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/:id');
request.setMethod('GET');
HttpResponse response = http.send(request);
String strRes = '';
System.debug('response code: ' + response.getStatusCode());
System.debug('reseponse body: ' + response.getBody());
//If the response is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
Map<String, Object> animals = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
//Cast the value in the animals' key as list.
// List<Object> animals = (List<Object>) results.get('animal');
System.debug('received the following: ' + animals);
strRes = String.valueOf(animals.get('name'));
System.debug('strRes: ' + strRes);
} // end if
return strRes;
} // end method
} // end class
<---AnimalLocatorMock--->
@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
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":"dog","eats":"meat","says":"arf arf!"}}');
response.setStatusCode(200);
return response;
} // end method
} // end class@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
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":"dog","eats":"meat","says":"arf arf!"}}');
response.setStatusCode(200);
return response;
} // end method
} // end class
<----AnimalLocatorTest----->
@isTest
public 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 result = AnimalLocator.getAnimalNameById(1);
String expectedResult='dog';
System.assertEquals(result, expectedResult);
} // end method
} // end class {@isTest
public 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 result = AnimalLocator.getAnimalNameById(1);
String expectedResult='dog';
System.assertEquals(result, expectedResult);
} // end method
} // end class {
May I ask some help because of this error
"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."
All classes were saved without errors. Here are my codes:
<---AnimalLocator class--->
public class AnimalLocator {
public static String getAnimalNameById(Integer id) {
Http http = new Http();
HttpRequest request = new HTTPRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/:id');
request.setMethod('GET');
HttpResponse response = http.send(request);
String strRes = '';
System.debug('response code: ' + response.getStatusCode());
System.debug('reseponse body: ' + response.getBody());
//If the response is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
Map<String, Object> animals = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
//Cast the value in the animals' key as list.
// List<Object> animals = (List<Object>) results.get('animal');
System.debug('received the following: ' + animals);
strRes = String.valueOf(animals.get('name'));
System.debug('strRes: ' + strRes);
} // end if
return strRes;
} // end method
} // end classpublic class AnimalLocator {
public static String getAnimalNameById(Integer id) {
Http http = new Http();
HttpRequest request = new HTTPRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/:id');
request.setMethod('GET');
HttpResponse response = http.send(request);
String strRes = '';
System.debug('response code: ' + response.getStatusCode());
System.debug('reseponse body: ' + response.getBody());
//If the response is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
Map<String, Object> animals = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
//Cast the value in the animals' key as list.
// List<Object> animals = (List<Object>) results.get('animal');
System.debug('received the following: ' + animals);
strRes = String.valueOf(animals.get('name'));
System.debug('strRes: ' + strRes);
} // end if
return strRes;
} // end method
} // end class
<---AnimalLocatorMock--->
@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
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":"dog","eats":"meat","says":"arf arf!"}}');
response.setStatusCode(200);
return response;
} // end method
} // end class@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
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":"dog","eats":"meat","says":"arf arf!"}}');
response.setStatusCode(200);
return response;
} // end method
} // end class
<----AnimalLocatorTest----->
@isTest
public 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 result = AnimalLocator.getAnimalNameById(1);
String expectedResult='dog';
System.assertEquals(result, expectedResult);
} // end method
} // end class {@isTest
public 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 result = AnimalLocator.getAnimalNameById(1);
String expectedResult='dog';
System.assertEquals(result, expectedResult);
} // end method
} // end class {
Sorry I didnt shared the main class.
Thanks.
All Answers
Try this class once
AnimalLocatorMock AnimalLocatorMock AnimalLocatorTest
Let me know if you have any issues.
Mark it as best answer if it works..
Thanks.
Sorry I didnt shared the main class.
Thanks.
AnimalLocator
response.setBody('{"animal":{"id":2,"name":"bear","eats":"berries, campers, adam seligman","says":"yum yum"}}');?