skyscreamer / JSONassert

Write JSON unit tests in less code. Great for testing REST interfaces.
http://jsonassert.skyscreamer.org
Apache License 2.0
989 stars 198 forks source link

More verbose diff view on assertion error #91

Open semisiu opened 6 years ago

semisiu commented 6 years ago

We'd like to have an ability to provide more verbose, or generally custom json diff view on assertion error (such like un*x diff -y does).

For example, given two jsons:

{
  "id": "101",
  "key": {
    "a": "value",
    "b": "xyz",
    "c": 201
  }
}

and:

{
  "id": "102",
  "key": {
    "a": "value",
    "d": [1, 2]
  }
}

calling:

DiffViewProvider diffViewProvider = new LinuxAlikeDiffViewProvider();
JSONAssert.assertEquals(json1, json2, true, diffViewProvider);

would throw an AssertionError with original message and additionally (optionally) more verbose diff:

java.lang.AssertionError: id
Expected: 101
     got: 102
 ; key
Expected: b
     but none found
 ; key
Expected: c
     but none found
 ; key
Unexpected: d

Actual json vs expected json:
{                        {
  "id": "101",        |    "id": "102",
  "key": {                 "key": {
    "a": "value",            "a": "value",
    "b": "xyz",       |      "d": [1, 2]
    "c": 201          <
  }                        }
}                        }

More json-conscious diff:

{                        {
  "id": "101",        |    "id": "102",
  "key": {                 "key": {
    "a": "value",            "a": "value",
    "b": "xyz",       <
    "c": 201          <
                      >      "d": [1, 2]
  }                        }
}                        }
Kamil-Benedykcinski commented 6 years ago

Good idea!

bakomchik commented 6 years ago

Hi folks, it would be cool if you add information about type of expected and got objects here org.skyscreamer.jsonassert.JSONCompareResult#describe because sometimes we got :

java.lang.AssertionError: id
Expected: 101
     got: 101

in case of different types

carterpage commented 2 years ago

This is one of a few issues about printing the actual objects that failed in more detail. This can blow up the results for people who have massive JSON sets, but I can see it being friendly for smaller sets. I'm less inclined to add extensions into the core methods, but I wonder if there is a pattern that we can recommend for test callers instead. Once you know the test fails, there are probably a variety of useful tools for printing out the entire JSON objects.

takanuva15 commented 7 months ago

I support adding an option to make these assert messages more verbose (or at minimum, print out the whole jsons consecutively).

I'm using this library to verify that my Mongo queries are built correctly, and it's a lot easier to just compare the raw Mongo JSON side-by-side instead of trying to compare what keys are or aren't present between the two queries.

dominictobias commented 6 months ago

you are lucky that you have a diff and not just a generic error stack trace when they don't match :/