Class JsonAssert


  • public class JsonAssert
    extends java.lang.Object
    Assertion to compare JSON documents. Comparison is not strict:
    • formatting differences are ignored
    • order of elements in objects {} is not verified
    • objects can contain more elements than expected, for example {"one":1, "two":2} matches {"one":1}
    • order of elements in arrays [] is not verified by default, for example [1, 2] matches [2, 1]. This mode can be disabled with withStrictArrayOrder()
    • timezones in datetime values are not strictly verified, for example {"foo": "2015-01-01T13:00:00+2000"} matches {"foo": "2015-01-01T10:00:00-1000"}. This feature can be disabled with withStrictTimezone()

    Usage

     String actual = "{}";
     String expected = "{}";
     JsonAssert.assertJson(actual).isSimilarTo(expected);
     

    Expected JSON document can be loaded from URLs:

     String actual = "{}";
     JsonAssert.assertJson(actual).isSimilarTo(getClass().getResource("MyTest/expected.json"));
     
    Since:
    5.2
    • Method Detail

      • withStrictTimezone

        public JsonAssert withStrictTimezone()
      • withStrictArrayOrder

        public JsonAssert withStrictArrayOrder()
      • ignoreFields

        public JsonAssert ignoreFields​(java.lang.String... ignoredFields)
      • isSimilarTo

        public JsonAssert isSimilarTo​(java.lang.String expected)
      • isSimilarTo

        public JsonAssert isSimilarTo​(java.net.URL expected)
      • assertJson

        public static JsonAssert assertJson​(java.lang.String actualJson)
      • assertJson

        public static JsonAssert assertJson​(java.net.URL actualJson)