0

Trying to test equality of two Maps (including order) by turning them into lists beforehand. There are probably better ways to do it, but I'd like to know why this error comes up. Here is the test:

@Test
public void sortedEntriesTest() {
    List<Map.Entry<String, AtomicInteger>> actualList = stream.sortedEntries(stream.getMap());
    List<Map.Entry<String, AtomicInteger>> expectedList = 
                                              expectedMap.entrySet()
                                                         .stream()
                                                         .sorted(Comparator.comparingInt(e -> -e.getValue().get()))
                                                         .collect(Collectors.toList());
    Assert.assertThat(expectedList, is(actualList));
}

Here is the error:

java.lang.AssertionError: 
Expected: is <[file=1, for=1, project=1, is=1, an=1, just=1, example=1, this=2]>
 but: was <[file=1, for=1, project=1, is=1, an=1, just=1, example=1, this=2]>
Expected :is <[file=1, for=1, project=1, is=1, an=1, just=1, example=1, this=2]>

Actual   :<[file=1, for=1, project=1, is=1, an=1, just=1, example=1, this=2]>
Karl
  • 223
  • 1
  • 2
  • 11

2 Answers2

2

Try

Assert.assertThat(expectedList, is(equalTo(actualList)));

instead.

SeverityOne
  • 2,476
  • 12
  • 25
  • Still won't work error is the same, just with expected list in a different order – Karl Apr 13 '18 at 07:47
  • Could you make your code a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) then? Something that we can copy and paste into an IDE to run it. – SeverityOne Apr 13 '18 at 08:21
  • You can literally try to compare two Maps. One of them I turn into a List of entries and sort it by value (descending) with the sortedEntries static method. The others I turn into a list and sort the way you see in the code. I would be happy to compare them in any working way at this point. – Karl Apr 13 '18 at 08:34
  • Make an MCVE, put it here, and I'll put it in my IDE. If you want help, put some effort in it. – SeverityOne Apr 13 '18 at 09:25
  • 1
    As a matter of fact @SeverityOne making a MCVE out of this code will take me half an hour so I think I will just give up this approach entirely. Thanks for your help anyway – Karl Apr 13 '18 at 09:45
1

Explanation:

You are comparing references of two different objects, which are (just as the objects) different. That is why You are getting the AssertionError - first reference is not the second reference.

Solution:

Use the equals method (link to the Java documentation for List.equals()), and it will compare the contents of the lists, also by calling the Map's equals method.

Assert.assertTrue(expectedList.equals(actualList));

Documentation on Assert.assertTrue

Also, check this StackOverflow question and the first (selected) answer - comparing two maps.

Edit

Since You told that the error is still here, then it might be a problem in the list's items. You should check how Map.Entry instances in the expectedList and actualList are being created. Their actual types might be different, since the Map.Entry is just an interface.

Also, I suggest You to use a simpler method of getting the desired values for comparison.

Aleksandar
  • 3,558
  • 1
  • 39
  • 42
  • Wouldn't it be quicker and clearer to call `Assert.assertTrue(expectedList, actualList);` instead? – SeverityOne Apr 13 '18 at 05:42
  • @SeverityOne if You check [the docs for the `assertTrue` with two arguments](http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertTrue(java.lang.String,%20boolean)), You will see that Your solution is not possible. – Aleksandar Apr 13 '18 at 07:15
  • Sorry, I meant `assertEquals`. – SeverityOne Apr 13 '18 at 07:27
  • @SeverityOne I wrote my answer with `assertTrue` method, since this way it is **more clear** that the `List`'s `equals` method is invoked. Besides, [the documentation on assertEquals](http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertEquals(java.lang.Object,%20java.lang.Object)) doesn't say how the two objects are compared, though I suppose the `equals` method is also called. – Aleksandar Apr 13 '18 at 08:06
  • In my experience, more text means less clarity. There's nothing unclear about `assertEquals`, whereas with `assertTrue(x.equals(y))` you're spreading the logic around. – SeverityOne Apr 13 '18 at 08:19
  • I think clarity is a subjective thing, so we will let the OP and the rest of the coders decide :) – Aleksandar Apr 13 '18 at 08:21
  • Thank you for you answer. Still not working. Java.lang.AssertionError, and it no longer shows the lists, "is, "was", etc. – Karl Apr 13 '18 at 08:28
  • Sadly I did not. I think I will resort to a direct "hand" testing of each entry with a custom entry I supply. – Karl Apr 13 '18 at 09:44