7

I am using assertJsonEquals of JsonUnit

I do the following in my code:

assertJsonEquals(resource("ExpecedResponse.json"),
                         ActualResponse, when(IGNORING_ARRAY_ORDER));

The ActualResponse has the response from a HTTP POST.

The ExpectedResponse.json is a json file with some fields as follows for e.g:

{
  "columnNames": [
    "date",
    "signalType",
    "userId",
  ],
  "values": [
    [
      222555888,
      "OUT",
      "000-000-111-444"
    ],
    [
      333666999,
      "IN",
      "000-000-222-333"
    ],
  ],
  "lastUpdatedTimestamp": "2018-01-26T00:00:00Z"
}

I compare the two responses with assertJsonEquals.

My question is: How do I tell it to ignore checking the lastUpdatedTimestamp field but check everything else with assertJsonEquals or any other library that you can recommend?!

If I remove the lastUpdatedTimestamp from ExpectedResponse.json, then it complains that it is missing!

Would appreciate your help, thanks.

Lukas
  • 13,606
  • 9
  • 31
  • 40
Saffik
  • 911
  • 4
  • 19
  • 45

3 Answers3

7

You could use the library https://github.com/skyscreamer/JSONassert that has an assert method that allows customization.

Here's an example of a test that is passing (and such ignoring the value of the time field)

  @Test
  public void test() throws JSONException {
    JSONAssert.assertEquals("{x: 1, time:123}",
                            "{x: 1, time:234}",
                            new CustomComparator(
                              JSONCompareMode.STRICT,
                              Customization.customization("time", // json path you want to customize
                                                          new ValueMatcher() {
                                @Override public boolean equal(Object o1, Object o2) {
                                  return true; // in your case just ignore the values and return true
                                }
                              }))
    );
  }

Here's the link to the javadoc of the assertEquals method that I am using in the example: http://jsonassert.skyscreamer.org/apidocs/org/skyscreamer/jsonassert/JSONAssert.html#assertEquals-java.lang.String-java.lang.String-org.skyscreamer.jsonassert.comparator.JSONComparator-

user2456718
  • 216
  • 1
  • 4
2

As the documentation says:

assertJsonEquals(
   "{\"root\":{\"test\":1, \"ignored\": 2}}", 
   "{\"root\":{\"test\":1, \"ignored\": 1}}", 
   whenIgnoringPaths("root.ignored")
);
Lukas
  • 13,606
  • 9
  • 31
  • 40
  • can I provide multiple options? I want to use `whenIgnoringPaths("root.ignored")` and `JsonAssert.when(Option.IGNORING_ARRAY_ORDER)` – Swagatika Jun 11 '19 at 22:18
0

To ignore only one field add '@IGNORE" to the expected json and then set setIgnorePlaceholder as below

JsonAssert.setTolerance(0.0000001);
JsonAssert.setOptions(IGNORING_ARRAY_ORDER);
JsonAssert.setIgnorePlaceholder("@IGNORE"); //You have to tag the ignored values in ExpecedResponse.json with "@IGNORE"

assertJsonEquals(resource("ExpecedResponse.json"),
                         ActualResponse);