• Peter Thorson
  • NEWBIE
  • 10 Points
  • Member since 2015
  • Senior Consultant

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
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;
    }


 
On this module, specifically on the Apex Rest Callout unit, I feel like this following challenge instruction could be misleading.

    •    The 'getAnimalNameById' method must call https://th-apex-http-callout.herokuapp.com/animals/:id, using the ID passed into the method. The method returns the value of the 'name' property (i.e., the animal name).

If someone physically copies that link, removes id but leaves in : then the challenge review will not pass because the request link will post as 
https://th-apex-http-callout.herokuapp.com/animals/:99
instead of the correct 
https://th-apex-http-callout.herokuapp.com/animals/99

The first incorrect link will still perform get for the request, but just blank values in the json response.