I am trying to do the Apex REST Callout Trailhead.

One of the challenge requirements is:

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

However I'm getting this error : a string is not a string??

The returned string is not a string?

Here is the code doing the deserializing :

map<String, Object> results = (map<String, Object>)JSON.deserializeUntyped(response.getBody());
list<Object> animals = (list<Object>)results.get('animals');
returnedString = (string)JSON.serialize(animals.get(id-1));

Here is the test class that asserts :

AnimalLocator ANML = new AnimalLocator();
Test.SetMock(HttpCallOutMock.class, new AnimalLocatorMock());
string result = ANML.getAnimalNameById(3);

string expectedResult = 'scary bear';

System.assertEquals(result, 'scary bear');
System.assertEquals(result, expectedResult);

And here is the mock callout class that sets the body of the request (I do not know precisely how it works, I picked it up from developer forums) :

HttpResponse response = new HttpResponse();
    response.setHeader('Content-Type', 'application/json');
    response.setBody('{"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken"]}');

Please refer to https://th-apex-http-callout.herokuapp.com/animals/ for the JSON source.

  • 3
    Likely to be that you have got double quotes actually in the string. Run this System.assertEquals('"abc"', 'abc'); and you will get that sort of failure message.
    – Keith C
    Nov 1, 2016 at 10:56
  • Hi, I updated my question.
    – fraglord
    Nov 1, 2016 at 11:32
  • Even if someone has the full answer to your problem, I hope they would not post it here as Trailhead progress is mean to be through your own understanding and work.
    – Keith C
    Nov 1, 2016 at 11:38
  • 4
    With more code posted yes asking is maybe more appropriate. First thing to say is that you have the expected and actual values reversed in System.assertEquals(result, 'scary bear'); which makes the assertion failure message misleading. My hint to you of the cause of the problem is that you would generally only ever serialize or deserialize in a method and you should learn about casting.
    – Keith C
    Nov 1, 2016 at 11:51
  • 2
    Found out the problem : method was not static. Changed it and how it was called in testclass and I have passed the trailhead. Thanks for showing me something new today :)
    – fraglord
    Nov 1, 2016 at 12:29


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.