• gil shani
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Hi All,

May I ask some help because of this error

"Challenge Not yet complete... here's what's wrong: 
Executing the 'getAnimalNameById' method on 'AnimalLocator' failed. Make sure the method exists with the name 'getAnimalNameById', is public and static, accepts an Integer and returns a String."

All classes were saved without errors. Here are my codes:

<---AnimalLocator class--->
public class AnimalLocator {
    
    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);
        
        String strRes = '';
        System.debug('response code: ' + response.getStatusCode());
        System.debug('reseponse body: ' + response.getBody());
        
        //If the response is successful, parse the JSON response.
        if (response.getStatusCode() == 200) {
            Map<String, Object> animals = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
        
        //Cast the value in the animals' key as list.
       // List<Object> animals = (List<Object>) results.get('animal');
        System.debug('received the following: ' + animals);
        strRes = String.valueOf(animals.get('name'));
        System.debug('strRes: ' + strRes);
        }   // end if
        
        return strRes;
    }   // end method
}   // end classpublic class AnimalLocator {
    
    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);
        
        String strRes = '';
        System.debug('response code: ' + response.getStatusCode());
        System.debug('reseponse body: ' + response.getBody());
        
        //If the response is successful, parse the JSON response.
        if (response.getStatusCode() == 200) {
            Map<String, Object> animals = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
        
        //Cast the value in the animals' key as list.
       // List<Object> animals = (List<Object>) results.get('animal');
        System.debug('received the following: ' + animals);
        strRes = String.valueOf(animals.get('name'));
        System.debug('strRes: ' + strRes);
        }   // end if
        
        return strRes;
    }   // end method
}   // end class

<---AnimalLocatorMock--->
@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":"dog","eats":"meat","says":"arf arf!"}}');
        response.setStatusCode(200);
        return response;
        
    }   // end method
}   // end class@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":"dog","eats":"meat","says":"arf arf!"}}');
        response.setStatusCode(200);
        return response;
        
    }   // end method
}   // end class


<----AnimalLocatorTest----->
@isTest
public class AnimalLocatorTest  {
    
    @isTest static void testGetCallout() {
        //Set mock callout class
        Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
        //This causes a fake response to be sent
        //from the class that implements HttpCalloutMock
        String result = AnimalLocator.getAnimalNameById(1);
        String expectedResult='dog';
        System.assertEquals(result, expectedResult);

    }   // end method
    
}   // end class {@isTest
public class AnimalLocatorTest  {
    
    @isTest static void testGetCallout() {
        //Set mock callout class
        Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
        //This causes a fake response to be sent
        //from the class that implements HttpCalloutMock
        String result = AnimalLocator.getAnimalNameById(1);
        String expectedResult='dog';
        System.assertEquals(result, expectedResult);

    }   // end method
    
}   // end class {
 
i have created
1.AnimalLocator User-added image



2.AnimalLocatorMock
User-added image

3.AnimalLocatorTest

User-added image


error i am getting

User-added image

please help me