You need to sign in to do that
Don't have an account?

Apex REST Callouts - Create an Apex class that calls a REST endpoint and write a test class
i have created
1.AnimalLocator
2.AnimalLocatorMock

3.AnimalLocatorTest

error i am getting

please help me
1.AnimalLocator
2.AnimalLocatorMock
3.AnimalLocatorTest
error i am getting
please help me
You need to add your end point in remote site settings.
Go to set up > Remote Site Settings > New
Enter your end point and save it.
Mark this as best answer if this resolves your query.
Regards
Sampath
Your end point is 'https://th-apex-http-callout.herokuapp.com/animals'
Mark this as best answer if this answers your query.
Regards
Sampath
https://developer.salesforce.com/trailhead/visualforce_mobile_salesforce1/visualforce_mobile_salesforce1_actions_global
The 'BusinessCard' page must display the current user's first name, last name, email, phone, and title information using the appropriate Visualforce global variables.
The page should have an input field to enter an email address and a 'Send' button that allows users to quickly email their contact information to others. Implementing this requirement is optional for passing this challenge.
please i need code to this part .....
Error : Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.NullPointerException: Attempt to de-reference a null object
Class : AnimalLocator
public class AnimalLocator {
public static string getAnimalNameById(Integer x){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
Map<String, Object>animal= new Map<String,Object>();
HttpResponse response=http.send(request);
if (response.getStatusCode() == 200) {
// Deserializes the JSON string into collections of primitive data types.
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
animal = (Map<String, Object>) results.get('animal');
}
return(String)animal.get('name');
}
}
Class : AnimalLocatorTest
@isTest
private class AnimalLocatorTest{
@isTest static void AnimalLocatorMock1() {
Test.SetMock(HttpCallOutMock.class, new AnimalLocatorMock());
string result=AnimalLocator.getAnimalNameById(3);
string expectedResult='chicken';
System.assertEquals(result, expectedResult);
}
}
Class : AnimalLocatorMock
@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest request) {
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;
}
}
Can some one help me here please.
Check this out.
Code for AnimalLocator apex class Code for Test Class - AnimalLocatorTest
Code for AnimalLocatorMock
Thanks,
Santhosh
but im getting this error message:
Method does not exist or incorrect signature: [List<Object>].get(String)
Is there another way to get the value of a List<Object> element?
Thanks in advance.
I am trying solve trailhead problem but I am getting below message .
Method does not exist or incorrect signature: Test.setMock(Type, AnimalLocatorMock) when I am trying to save test class
public class AnimalLocator {
public static string getAnimalNameById(Integer animalId){
Http http=new Http();
HttpRequest req=new HttpRequest();
req.setEndPoint('https://th-apex-http-callout.herokuapp.com/animals/' + animalId);
req.setMethod('GET');
HttpResponse res=http.send(req);
Map<String,Object> animals = new Map<String,Object>();
if (res.getStatusCode() == 200) {
Map<String,Object> results = (Map<String,Object>)JSON.deserializeUntyped(res.getBody());
animals = (Map<String,Object>)results.get('animal');
}
else{System.debug('The status code returned was not expected: ' + res.getStatusCode() + ' ' + res.getStatus());}
return (string)animals.get('name');
}
}
@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
global HttpResponse respond(HttpRequest request){
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setBody('{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');
//response.setBody('{"animal":{"id":1,"name":"chicken"}}');
response.setStatusCode(200);
return response;
}
}
@isTest
private class AnimalLocatorTest{
@isTest static void AnimalsCalloutsTest() {
Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
String ALresult = AnimalLocator.getAnimalNameById(1);
String expectedValue = 'chicken';
System.assertEquals(ALresult, expectedValue);
}
}
Method does not exist or incorrect signature: Test.setMock(Type, AnimalLocatorMock) when I am trying to save test class.
Can some one help me here please.
follow this steps to tackle the challenge
Step 1 : go to Remote site controllers in security control
Enter External site url https://th-apex-http-callout.herokuapp.com/animals/:id
save
step 2 : Apex class
step 3 : test class
step 4 : Devloper console
click on test : choose Asyncronous test
click on new run
choose the Animal locator mock
animal locator test
Run
that all
challenge is done
I decided to change up the JSON file a bit and make it a bit more challenging to write a code that actually made use of the input. I am still learning and would really appreciate some feedback on my code if anyone has any input.
Aso, please note that the code I am posting will result in 100% code coverage but WON'T complete the challenge. It was done stricktly by me as a learning excersise.
Method does not exist or incorrect signature: Test.setMock(Type, AnimalLocatorMock) when I am trying to save test class
Please check if in your org is there any class name 'test'. Delete that class or change the name. This will work.
I suppose it should be three with Chicken as Expected results but when I ran with 3, it accepted the code and Tests were passed. Please help if you know.
100% working.
Issue with Line : request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + id);
Please Select this as Best Answer if working properly.
Thanks in Advance :)
response.setBody(' {"animal":[{"id":1,"name":"Chicken","eats":"Grain","says":"Cluck"},?
Go to set up > Remote Site Settings > New
Remote Site Name = animals_http
Remote Site URL = https://th-apex-http-callout.herokuapp.com
Description = Trailhead animal service: HTTP
Click on Save (Screenshot is below)
Code for 'AnimalLocator' Class Code for 'AnimalLocatorMock' Class Code for 'AnimalLocatorTest' Class
Now, Developer Console > Test > Run All (Overall coverage should 100%)
Now Go to Trailhead module and click on 'Check Challenge'
@Ankit Jain 2909
WOrked Nice ly THanks a lot...
when i tried the same above code im getting below error:
Invalid conversion from runtime type List<ANY> to Map<String,ANY>
FATAL_ERROR Class.AnimalLocator.getAnimalNameById: line 14, column 1
Code:
public class AnimalLocator {
public static string getAnimalNameById(integer animalNameId){
http h= new http();
httprequest req= new httprequest();
req.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+animalNameId);
req.setmethod('GET');
httpresponse res =h.send(req);
String strResp = '';
// map<string,object> animals =new Map<String,Object>();
//list<object> animals =new list<object>();
if(res.getStatusCode() == 200){
map<string,object> results = (map<string,object>) JSON.deserializeUntyped(res.getBody());
system.debug(''+results);
map<string,object> animals =(map<string,object>) results.get('animal');
system.debug(''+animals);
strResp= string.valueof(animals.get('name'));
}else
{System.debug('The status code returned was not expected: ' + res.getStatusCode() + ' ' + res.getStatus());}
return strResp;
}
}
Mock test:
@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest request){
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setBody(' {"animal":[{"id":1,"name":"Chicken","eats":"Grain","says":"Cluck"},{"id":2,"name":"Dog","eats":"Chicken","says":"Woof"}]} ');
response.setStatusCode(200);
return response;
}
}
Test Class:
@isTest
public with sharing class AnimalLocatorTest {
@isTest
static void testGetCallout(){
Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
String resul1t =AnimalLocator.getAnimalNameById(1);
string expectedresult ='Chicken';
System.assertEquals(resul1t,expectedResult);
resul1t = AnimalLocator.getAnimalNameById(4);
expectedResult = 'Could not find an Animal with a matching ID';
System.assertEquals(resul1t,expectedResult);
}
}
Not able to understand where i went wrong.
Regards,
Harsha.
I didn't understand how this JSON was built in the AnimalLocatorMock class, could someone explain it to me? Thank you.
'{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}'
Step-1
AnimalLocator.apxc
public class AnimalLocator {
/* Method to GET response from a web service */
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);
//If request successful Parse the response
Map<String, Object> animals = new Map<String, Object>();
if(response.getStatusCode() == 200){
//Deserialize the JSON string into collection object
Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
animals = (Map<String, Object>) results.get('animal');
system.debug('Animal Object->>>>'+animals);
system.debug('Animals Name->>>>'+animals.get('name'));
}
else{
system.debug('The status code return was not expected'+response.getStatusCode());
}
return (string)animals.get('name');
}
}
Step-2
AnimalLocatorMock.apxc
@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
//implement interface method
global HttpResponse respond(HttpRequest request){
//Creating 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;
}
}
Step-3
AnimalLocatorTest.apxc
@isTest
public class AnimalLocatorTest {
@isTest static void getAnimalNameByIdTest(){
Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
string results = AnimalLocator.getAnimalNameById(1);
string expectedResult = 'chicken';
system.assertEquals(expectedResult, results);
/*string results = AnimalLocator.getAnimalNameById(2);
string expectedResult2 = 'duck';
system.assertEquals(expectedResult2, results, 'This is not expected...');*/
}
}
Step-4
Click Run Test in the 'AnimalLocatorTest' class ----> Go to 'AnimalLocator' class ----> Click on Code Coverage dropdown ----> Select All Tests
-----> It will show you 100% Code Coverage.
Step-5
After that 100% Code Coverage how do you verify that you actually get correct response or not. Simple, Go to Logs Tab in Developer Console ----> Double click the recent envocation class ----> Click Debug only check box ----> Now you can see the out put.
If you want to pass more than one records you need to array type inside JSON string like-
'{"animal":[{"id":1, "name":"test1", "says":"Hello",},{"id":2, "name":"test2", "says":"World"}]}'; in this case we pass list of records through JSON string.
For more information refer this resource- https://json2apex.herokuapp.com/
Hope it helps!!
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.
@Areno
i applied same code but i am getting this error.