Skip to main content
Hello Community,

 

Create an Apex class that calls a REST endpoint to return the name of an animal, write unit tests that achieve 100% code coverage for the class using a mock response, and run your Apex tests.

 

Create an Apex class:

 

Name: AnimalLocator

 

Method name: getAnimalNameById

 

The method must accept an Integer and return a String.

 

The 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)

 

Create a test class:

 

Name: AnimalLocatorTest

 

The test class uses a mock class called AnimalLocatorMock to mock the callout response

 

Create unit tests:

 

Unit tests must cover all lines of code included in the AnimalLocator class, resulting in 100% code coverage

 

Run your test class at least once (via Run All tests the Developer Console) before attempting to verify this challenge.

 

my code is,

 

public class  AnimalLocator {

 

     public static string getAnimalNameById(integer animalId) {

 

         string animalName;

 

         Http http = new Http();

 

         HttpRequest request = new HttpRequest();

 

         request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+animalId);

 

         request.setMethod('GET');

 

         HttpResponse response = http.send(request);

 

         // If the request is successful, parse the JSON response.

 

         if (response.getStatusCode() == 200) {

 

             Map<String, Object> result = (Map<String, Object>) 

 

                 JSON.deserializeUntyped(response.getBody());

 

             Map<String, Object> animal = (Map<String, Object>)result.get('animal');

 

              animalName = string.valueOf(animal.get('name'));

 

         }

 

         return animalName;

 

     }

 

}

 

Test Code is

 

@isTest

 

Private class AnimalLocatorTest {

 

    @isTest static void getAnimalNameById_Test(){

 

        Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());

 

        string response = AnimalLocator.getAnimalNameById(1);

 

        system.assertEquals('chicken', response);

 

    }

 

}

 

but it throws the error saying * Challenge not yet complete. 

 

 

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.*
2 个回答
  1. 2021年5月21日 12:49
    Hi MO,

     

    Try this

     

    public class AnimalLocator{

     

        public static String getAnimalNameById(Integer x){

     

            Http http = new Http();

     

            HttpRequest req = new HttpRequest();

     

            req.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + x);

     

            req.setMethod('GET');

     

            

     

            HttpResponse res = http.send(req);

     

            if (response.getStatusCode() == 200) {

     

                Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());

     

                Map<String, Object> animal = (Map<String, Object>) results.get('animal');

     

                return (String)animal.get('name');

     

            }

     

        }

     

    }

     

    Thanks,

     

    Dinesh