Hi, in unit tests we compare 2 JsonObjects (from `...
# server
l
Hi, in unit tests we compare 2 JsonObjects (from
kotlinx.serialization.json
) but in some cases we'd like to ignore some keys (generated IDs, timestamps...) Today we have a simple
Copy code
assertEquals(object1, object2)
Do you see any way to ignore a key / or remove a key from a JsonObject before the comparison?
r
Use a different assertion library. e.g. kotest's assertion library: https://kotest.io/docs/assertions/json/content-json-matchers.html#shouldequalspecifiedjson You don't need to use the framework to use an assertion library; so long as it throws
AssertionError
if an assertion fails it will work in JUnit or whatever.
JsonUnit is another option
l
Thanks @Rob Elliot! Actually we also use kotest, but I don't see how
shouldEqualSpecifiedJson
/ lenient comparison could help?
r
I assumed you were specifying the expected side of the comparison explicitly, so could just leave out the fields you don't care about. If you are comparing two objects which both have the fields you want to ignore then JsonUnit's Ignoring paths might work?
a
It's possible to remove keys from JsonObjects, although it's not obvious. See https://github.com/Kotlin/kotlinx.serialization/issues/2308
thank you color 1
185 Views