0

I am writing Unit test using Junit 4 to compare two List I have two lists of tuples of type java.util.List

The tuple order can be different inside the List. I would like to compare these two Lists.

What i have done

assertEquals(new HashSet(List1), new HashSet(List2));

Assertion is failing saying

junit.framework.AssertionFailedError: 
Expected :[2    second  38.46   162.417 361.583 112.331, 1  first   157.577 154.985 135.126 171.198]
Actual   :[2    second  38.46   162.417 361.583 112.331, 1  first   157.577 154.985 135.126 171.198]

I can't see any difference between two, how to compare this?

Edit: Even I am comparing like

assertEquals(expectedCanonmap, getOutput(out));

it is saying

junit.framework.AssertionFailedError: 
Expected :[1    first   157.577 154.985 135.126 171.198, 2  second  38.46   162.417 361.583 112.331]
Actual   :[1    first   157.577 154.985 135.126 171.198, 2  second  38.46   162.417 361.583 112.331]
tbodt
  • 16,609
  • 6
  • 58
  • 83
nothing_authentic
  • 2,927
  • 3
  • 17
  • 22
  • 4
    What is printed is the toString() of each set. But two objects a and b having the same toString() are not necessarily equal. To be equal, a.equals(b) must return true (and vice versa). Have you implemented equals() and hashCode() in your Tuple class? Where is the code? – JB Nizet May 17 '14 at 05:42
  • Also, if you want to compare two lists, why do you need to stick them into a `HashSet` before comparing? Seems rather redundant to me – awksp May 17 '14 at 05:45
  • +1 for @user3580294 you should just sort these lists, then run `Assert.assertEquals` for them – Dmitry Ginzburg May 17 '14 at 05:47
  • I have not implemented the equals() and hashCode() in TupleClass. How to sort this list on the basis of what?? – nothing_authentic May 17 '14 at 06:02
  • 1
    You have to override `equals()` and `hashCode()` if you expect a `HashSet` and `assertEquals()` to work. – awksp May 17 '14 at 06:22
  • See also http://stackoverflow.com/questions/4055780/hashset-with-two-equals-object – Raedwald May 17 '14 at 09:20
  • 1
    See also http://stackoverflow.com/questions/3692426/hashset-does-not-seem-to-realize-that-two-objects-are-the-same – Raedwald May 17 '14 at 09:22

0 Answers0