From: Kerns, Bob <Bob.K<erns@fi...> - 2002-05-27 20:51:34
It's usually easy to compare collections by converting them to either a List
or a Set (depending on what sort of "equality" you are looking for).
assertEquals("c1 equals c2", new ArrayList(c1), new ArrayList(c2));
assertEquals("c1 equals c2", new HashSet(c1), new HashSet(c2));
As this illustrates, however, there is no single definititon of what
'equals' means for a collection (other than that the method has to be
defined and has to meet the contract for Object.equals(Object)!). You can
assertEquals("c1 equals c2", c1, c2);
But the meaning would depend entirely on the implementation of c1 and c2, in
ways which are probably not very helpful for testing.
--- Original Message ---
Date: Sun, 26 May 2002 17:15:19 -0700 (PDT)
From: Aidan Rourke <kms_junit@...>
Subject: [Junit-devel] Asserting If Collections Are The Same
I'm just starting to use JUnit and have a need to compare 2 collections (
the "expected result" and the "actual result") to see if they contain the
same items. I can write a method that does this for me (by looping through
the collections) and then using that method in an assertTrue(). But I was
wondering if the was some way to compare the 2 collections in JUnit itself.
I didn't notice any such thing after going through the Javadocs and mailing
lists, so I thought I'd post this message to see if anyone had done this
before and how you solved it.