function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdcDeveloper 12sfdcDeveloper 12 

Apex REST Callouts

I am not able to save my test 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;
    }


 
Peter ThorsonPeter Thorson
In you test class you have
string result = AnimalLocator.getAnimalNameById(0);

try capatilizing string:
String result = AnimalLocator.getAnimalNameById(0);

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:
 
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+id);
See the discussion on that (https://developer.salesforce.com/forums?communityId=09aF00000004HMGIA2#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Trailhead&criteria=ALLQUESTIONS&id=906F0000000MJi5IAG)
 
sfdcDeveloper 12sfdcDeveloper 12
Still Getting below Error:

Error: Compile Error: Method does not exist or incorrect signature: AnimalLocator.getAnimalNameById(Integer) at line 5 column 25