David Kubecka
03/13/2023, 4:27 PMmapOf(1 to [2,3])
and mapOf(1 to [3,2])
should compare the same.Joffrey
03/13/2023, 4:28 PMDavid Kubecka
03/13/2023, 4:28 PMephemient
03/13/2023, 4:29 PMJoffrey
03/13/2023, 4:31 PMThe inner list is returned from a 3rd party.So if you don't care about the order of the things returned by the third party lib, you could map the list to a set at that point (not in tests)
David Kubecka
03/13/2023, 4:32 PMJoffrey
03/13/2023, 4:39 PMwhat about other "advanced" matchers such as ignoring property values in an instanceThat could also be expressed in production code if it doesn't matter in production, by customizing equals or putting a subset of data class properties outside of the constructor. If some properties use some randomness (like randomly generated IDs), maybe you could inject some fixed seed in tests to solve that. If none of the above works, you can convert both sides of the
assertEquals()
to some other representation that matches what you want to take into account (mapping instances to some other type with a subset of the properties, mapping lists to sets, this kind of things)equals()
. I'm not sure what you're looking for exactly.CLOVIS
03/13/2023, 4:43 PMList
is that order is important. As long as it's a list, everything in the language will take the order of account.
Set
, however, does not declare that the order is important. Some implementations ignore the order (HashSet
), some take it into account (SortedSet
).David Kubecka
03/13/2023, 4:49 PMIf some properties use some randomnessA classic example would be DB generated ids. The method under test creates some data and saves it to DB. I'm then checking the content of the entities. Of course, I can either: • split the code to generating and saving parts and test only the generating part • set up specific DTO just for the test use case But both of them seem to be too much work for a simple task of ignoring a few properties.
CLOVIS
03/13/2023, 4:50 PMassertEquals(expected, actual.mapValues { _, v -> v.toSet() })
David Kubecka
03/13/2023, 4:52 PMignoring property values in an instance.Sorry...
CLOVIS
03/13/2023, 4:53 PMJoffrey
03/13/2023, 4:53 PMassertEquals(expected, actual.copy(id = "fixed_value"))
David Kubecka
03/13/2023, 4:54 PMJoffrey
03/13/2023, 4:55 PMequals
for a given type?Sure you can do that but that gets quite annoying once the structure has more levels of nestingTrue, that's why I usually prefer to do it "right" and fix the production code instead 🙂
David Kubecka
03/13/2023, 4:59 PMval expected = MyClass(prop1 = 1, prop2 = 2, prop3 = ignore())
val actual = MyClass(prop1 = 1, prop2 = 2, prop3 = 3)
expectThat(expected).equals(expected)
CLOVIS
03/13/2023, 5:05 PMDavid Kubecka
03/13/2023, 5:10 PMSzymon Jeziorski
03/15/2023, 9:24 AMval first = mapOf(1 to listOf(2, 3, mapOf("first" to listOf("1", "2", "3"))))
val second = mapOf(1 to (listOf(mapOf("first" to listOf("3", "1", "2")), 3, 2)))
assertThat(first)
.usingRecursiveComparison()
.ignoringCollectionOrder()
.isEqualTo(second) // this would pass