https://kotlinlang.org logo
Title
j

Jonathan Lennox

07/06/2022, 8:37 PM
I have a method that's declared as returning
Set<MyType>
. I'm writing a test that uses
shouldContainExactly
to test it. When I instantiate the return value as a
LinkedHashSet
it works, but when I instead instantiate it as a
HashSet
I get:
Disallowed: Set can be compared only to Set
java.lang.AssertionError: Disallowed: Set can be compared only to Set
May not compare HashSet with ArrayList
expected:<*> but was:<*>
What's going on here?
Oh, I guess
shouldContainExactly
requires the set to be ordered - what matcher should I use to match an unordered collection? (Also, that error message is terrible.)
l

LeoColman

07/06/2022, 8:43 PM
shouldContainExactlyInAnyOrder, I think
j

Jonathan Lennox

07/06/2022, 8:45 PM
Yep, that worked, thanks
I'll file a bug report about the error message.
:thank-you: 1
s

sam

07/06/2022, 9:12 PM
is shouldContainExactly the same as just shouldBe in this case?
j

Jonathan Lennox

07/07/2022, 6:36 PM
I think for shouldBe you'd have to construct the
LinkedHashSet
, and it'd fail if the actual type was a subtype? But I'm not sure
s

sam

07/07/2022, 7:46 PM
That makes sense