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
JJSHHSJJSHHS 

APEX REST Callout Challenge

Hello,

I have a question. In the challenge, we're asked to create the 'getAnimalNameById' method that accepts an Integer and returns a String.
The AnimalLocatorMock is NOT returning a string, instead it's returning a HTTPResponse. So, when I actually try to make the callout in the Test Method, I'm getting an error (Illegal assignment from String to System.HttpResponse).

I could change my 'getAnimalNameByID' method to return a HTTPResponse instead of a String. But, is this the correct way or am I missing something? I've pasted the code snippets below.
 
@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
    
    global HttpResponse respond(HTTPRequest request){
      //create 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;
    }

}
 
@isTest
public class AnimalLocatorTest {
    @isTest static void testGet(){
        //set the mockout class
        Test.setMock(HTTPCalloutMock.class, new AnimalLocatorMock());
        //Do the callout here
        HTTPResponse response = AnimalLocator.getAnimalNameById(1);
    }

}

 
Alexander TsitsuraAlexander Tsitsura
Hello,

You need ti rewrite AnimalLocator.getAnimalNameById method. When you do a callout, it returns HttpResponse, for retrieve string use getBody method.
 
HttpRequest req = new HttpRequest();
...

Http http = new Http();
HttpResponse res = http.send(req);
String response = res.getBody(); // String value

return response;


Thanks,
Alex
JJSHHSJJSHHS
Alex,

I've already done that -- that's not the issue. The issue is when it comes to testing it, I'm getting the error. Is there anway I could test it by implementing the HTTPCallOutMock interface. Copy of AnimalLocator below for your ref,
public class AnimalLocator {
    Static final String endPoint = 'https://th-apex-http-callout.herokuapp.com/animals/';
    
    public static String getAnimalNameById(Integer num){
       
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        String endPt = endPoint + String.valueOf(num);
        request.setEndpoint(endPt);
        request.setMethod('GET');
        HTTPResponse response = h.send(request);
        //200 is good response code
        if (response.getStatusCode() == 200){
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            Map<String,Object> attributes = (Map<String, Object>) results.get('animal');
            System.debug('Animal Name ->' + attributes.get('name'));
            return (String) results.get('name');
               
        }
        else return null;
              
    }

}

 
Alexander TsitsuraAlexander Tsitsura
Hi,

The problem in your test class. AnimalLocator.getAnimalNameById return String, not HTTPResponse.
 
@isTest
public class AnimalLocatorTest {
    @isTest static void testGet(){
        //set the mockout class
        Test.setMock(HTTPCalloutMock.class, new AnimalLocatorMock());
        //Do the callout here
        String response = AnimalLocator.getAnimalNameById(1);
       System.assertEquals(
          '{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}',
           response
       );
    }
}

Thanks, Alex