You need to sign in to do that
Don't have an account?
Kiran Pandiyan
How can I solve this error in Apex REST Callout challenge ?
Global class AnimalLocator { Global static String getAnimalNameById(Integer numb){ String response; Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+numb); request.setMethod('GET'); HttpResponse resp = http.send(request); if(resp.getStatusCode()==200){ Map<String,Object> results = (Map<String,Object>) JSON.deserializeUntyped(response.getBody()); Map<String,Object> animal = (Map<String,Object>)results.get('animal'); response = (String)animal.get('name'); } return response; } }I'm getting the error "Method does not exist or incorrect signature : void getBody() from String type "
Try below code for AnimalLocator class:-
public class AnimalLocator {
public class AnimalInstanceClass {
public Integer id;
public String name;
public String eats;
public String says;
}
public class JSONResponseClass{
public AnimalInstanceClass animal;
}
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);
system.debug('response: ' + response.getBody());
JSONResponseClass results = (JSONResponseClass) JSON.deserialize(response.getBody(), JSONResponseClass.class);
system.debug('results= ' + results.animal.name);
return(results.animal.name);
}
}
Let me know in case of any concerns.
Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.
Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com
All Answers
Try below code for AnimalLocator class:-
public class AnimalLocator {
public class AnimalInstanceClass {
public Integer id;
public String name;
public String eats;
public String says;
}
public class JSONResponseClass{
public AnimalInstanceClass animal;
}
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);
system.debug('response: ' + response.getBody());
JSONResponseClass results = (JSONResponseClass) JSON.deserialize(response.getBody(), JSONResponseClass.class);
system.debug('results= ' + results.animal.name);
return(results.animal.name);
}
}
Let me know in case of any concerns.
Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.
Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com
instead of
SO the previous problem is solved
Now the Challenge is Not yet complete... here's what's wrong:
"The Apex test class 'AnimalLocatorTest' does not appear to be using the AnimalLocatorMock class correctly."
My Test Class
How can I solve it ?My Mock Class
Try below code:-
1) AnimalLocatorTest class**********-
@IsTest
global class AnimalLocatorTest {
global HTTPresponse respond(HTTPrequest request) {
Httpresponse response = new Httpresponse();
response.setStatusCode(200);
response.setBody('{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');
return response;
}
@isTest
public static void testAnimalLocator() {
Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
//Httpresponse response = AnimalLocator.getAnimalNameById(1);
String s = AnimalLocator.getAnimalNameById(1);
system.debug('string returned: ' + s);
}
}
2) AnimalLocatorMock class**********-
@IsTest
global class AnimalLocatorMock implements HttpCalloutMock {
global HTTPresponse respond(HTTPrequest request) {
Httpresponse response = new Httpresponse();
response.setStatusCode(200);
response.setBody('{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');
return response;
}
}
Let me know in case of any concerns.
Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.
Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com
I just did something similar in Test.setMock line like you did and it worked for me.
Thanks for you help.