You need to sign in to do that
Don't have an account?
sfdcDeveloper 12
Apex REST Callouts
I am not able to save my test class:
Below is my test Class:
My Rest Class:
Below is my test Class:
@isTest private class AnimalLocatorTest{ @isTest static void AnimalLocatorMock1() { Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock()); string result = AnimalLocator.getAnimalNameById(0); String expectedResult = 'dog'; System.assertEquals(result,expectedResult ); } }My Mock 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('{"name": ["dog"]}'); response.setStatusCode(200); return response; } }
My Rest Class:
public class AnimalLocator{ public 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 ret; if(response.getStatusCode() == 200){ Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody()); List<Object> name = (List<Object>) results.get('name'); for (Object animal: name) { ret = (String)animal; } } return ret; }
try capatilizing string:
You might also want to make it a valid value.
Also the syntax for the API does not have ":" in so change that line to:
See the discussion on that (https://developer.salesforce.com/forums?communityId=09aF00000004HMGIA2#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Trailhead&criteria=ALLQUESTIONS&id=906F0000000MJi5IAG)
Error: Compile Error: Method does not exist or incorrect signature: AnimalLocator.getAnimalNameById(Integer) at line 5 column 25