0

I am just copying and pasting an exercise in the Trailhead API Integration tutorial and the system.assertEquals(expectedValue,actualValue) line keeps failing between the mocked JSON I am feeding versus the Actual, what's even strange is that the logs show that the Expected vs Actual values are the same

System.AssertException: Assertion Failed: Expected: {"animals":["majestic badger,"fluffy bunny","scary bear","chicken","mighty moose"]}, Actual: {"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken", "mighty moose"]}`

Any idea what tweaks I need to make to nudge the system assert? I tried editing and re-editing the whitespace but no luck there

Here's the @isTest class (copied from the tutorial)

@isTest
private class AnimalsCalloutsTest {
    @isTest static void testGetCallout() {
    //Create the mockk response based on a static resource
    StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
    mock.setStaticResource('GetAnimalResource');
    mock.setStatusCode(200);
    mock.setHeader('Content-Type','application/json;charset=UTF-8');
    //Associate the callout with a mock response
    Test.setMock(HttpCalloutMock.class, mock);
    //Call method to test
    HttpResponse result = AnimalsCallouts.makeGetCallout();
    //Verify mock response is not null
    System.assertNotEquals(null,result,
                 'The callout returned a null response');
    //Verify content type
    System.assertEquals('application/json;charset=UTF-8',
                       result.getHeader('Content-Type'),
                       'The content type value is not expected.');
    //Verify the array contains 3 items
    Map<String, Object> results = (Map<String,Object>)
        JSON.deserializeUntyped(result.getBody());
    List<Object> animals = (List<Object>) results.get('animals');
    System.assertEquals(3, animals.size(),
                       'The array should only contain 3 items');
}
    @isTest static void testPostCallout() {
        //set mock callout class
        Test.setMock(HttpCalloutMock.class, new AnimalsHttpCalloutMock());
        //This causes a fake response to be sent
        //from the class that implements HttpCalloutMock.
        HttpResponse response = AnimalsCallouts.makePostCallout();
        //Verify that the response received contains fake values
        String contentType = response.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = response.getBody();
        System.debug(response.getBody());
        String expectedValue = '{"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken", "mighty moose"]}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, response.getStatusCode());
    }
}
1
  • Check the hierarchy levels in both the arrays. print both arrays before asserting and share the results here
    – user55114
    Mar 25, 2018 at 10:12

2 Answers 2

1

"Actual, what's even strange is that the logs show that the Expected vs Actual values are the same"

They're not exactly the same.

Expected: {"animals":["

Actual: {"animals": ["

You don't have trailing space in expected value after {"animals":

Because you copied the code from the trailhead (which is correct) I'm assuming that your code is wrong (you wrote mocked value manually for example or during copy paste formatting delete that trailing space).

If this won't solve your issue please copy YOURS code.

1
  • Yep, I fixed the white space and the test still failed. Only when I printed out Expected and Actual value variables I saw that for some reason the Actual Value was missing a closing " after "majestic badger" - I changed the expected value to mimic the (incorrect) syntax of the actual value and the test ran successfully. I am not sure why the Actual Value is storing the incorrect JSON formatting
    – GPP
    Mar 25, 2018 at 18:20
0

Ok, I found the issue. The Run kept failing because the "JSON" getting returned and stored in the actual value is incorrect - The actual value is missing a " to close out the majestic badger object

A value: {"animals":["majestic badger,"fluffy bunny","scary bear","chicken","mighty moose"]}

I noticed that when I took @user55114's advice and printed out the Expected and Actual (E Value, A Value) and found the mismatch-- I am not sure why the JSON is returning in that formatting, the "majestic badger" is fully wrapped in quotes in the original URI Trailhead provides: https://th-apex-http-callout.herokuapp.com/animals

Edit: I found the issue with the JSON formatting-- it was an issue with how I had inputted the JSON in the AnimalsHttpCalloutMock class. I had missed the " on the "majestic badger" object, and when I fixed this and fixed the expected value, the code ran fine!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .