0

I have the following JSON String:

[
  {
    "id": 23425,
    "mailboxGroupId": 6659,
    "statusCode": "ACTIVE"
  },
  {
    "id": 23425,
    "groupId": 6659,
    "statusCode": "INACTIVE"
  }
]

I want to deserialize it into a list of two objects of Kotlin data class type:

val myList: List<Foo> = Gson().fromJson(response, ArrayList<Foo>().javaClass)

Next, I need to compare with AssertJ these two objects and assert that all fields except for statusCode are equal:

assertThat(myList.first()).isEqualToIgnoringGivenFields(myList.last(), "statusCode")

But when I run the test, I get an assertion error:

Expecting values:
<.....>
in fields:
  <["root", "entrySet", "values"]>

Looks like Gson adds "root", "entrySet", "values" during the deserialization, but I can't understand how can I exclude them?

Or I don't need to exclude them and just need to change my assertion?

Vitali Plagov
  • 722
  • 1
  • 12
  • 31
  • 1
    Possible duplicate of [How to use TypeToken + generics with Gson in Kotlin](https://stackoverflow.com/questions/33381384/how-to-use-typetoken-generics-with-gson-in-kotlin) – Laurence Apr 10 '19 at 15:16
  • maybe try to ignore "root", "entrySet", "values" fields along with "statusCode" ? not great but if it works ... – Joel Costigliola Apr 10 '19 at 21:40
  • @JoelCostigliola well, I tried to ignore "root, "entrySet" and "values" as well and it works. But when I remove "statusCode" from the list of ignored fields, then test doesn't fail. So I assume that POJOs are under the `root` field and thus the whole object is ignored when I ignore the `root` field. – Vitali Plagov Apr 11 '19 at 06:00
  • 1
    likely so Vitalii. If that is possible, you could try another approach and asserting directly the JSON data with https://github.com/lukas-krecan/JsonUnit wich supports ignoring values – Joel Costigliola Apr 11 '19 at 09:39

0 Answers0