Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to tell assertJsonEquals to ignore one field in comparing

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.

like image 998
Saffik Avatar asked May 04 '18 09:05

Saffik


People also ask

Is it possible to compare two objects recursively but ignore some fields?

But, there is no way I can compare two objects recursively by ignoring some fields. As per this discussion, it must be in development. How to still get my assert's return value to be compared recursively but ignoring some fields. Is it possible in any other library or can I do it somehow using AssertJ? Show activity on this post.

Can assertj do a recursive comparison and ignore fields?

With latest 'Recursive comparison api improvements' from AssertJ release 3.12.0 it's possible now to do a recursive comparison and ignore fields:

How to change the way two objects are compared in assert?

In order to change the way two objects are compared in an assert we only need change the behavior of one of them – the expect value (might change depending on the unit testing framework). And who is better in changing behavior of objects in tests than your friendly-neighborhood mocking framework?

Should I use two assertions in a test?

Using two asserts would work, at least for a time. The problem is that failing the first assert would cause an exception to be thrown leaving us with no idea if the second would have passed or failed. We can solve this issue by splitting the test into two tests — one test per assert.


1 Answers

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-

like image 143
user2456718 Avatar answered Sep 19 '22 09:09

user2456718