assertj-json icon indicating copy to clipboard operation
assertj-json copied to clipboard

Equals assert

Open giovannicandido opened this issue 6 years ago • 1 comments

Assert field by field is annoying and sometimes we want to compare the whole result.

Something like

val request = ChargeRequest(listOf(ItemRequest(1, "test", 1)), MetadataRequest("0001", ""))
val exectedJson = """
      {"metadataRequest":{"custom_id":"0001", "notification_url":""},"items":[{"amount":1,"name":"test","value":1}]}
    """.trimIndent()

JSONAssert.assertEquals(request.toJsonString, exectedJson, true)

In JSONAssert, there is the strict configuration. When true all fields must be equal and have the same property count. When false the fields that exist on the expectations must match, but extra fields are allowed. For example:

object a:

{
"foo":  "bar",
"bar": "foo"
}

object b:

{
"foo":  "bar"
}

In strict mode: a != b Strict mode false: a = b

giovannicandido avatar Jul 13 '19 16:07 giovannicandido

It is a valid use case, arguably this use case is easier to deal with if you have the power to deserialize the JSON in an object tree and do deep equality. Or just go cheap and compare the result of the toString on both sides.

I no longer maintain this library, nor have write access to the repository.

jrglee avatar Nov 07 '19 00:11 jrglee