Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssertEquals 2 Lists ignore order

That should be really simple question I believe. But somehow I can't find answer in Google.

Assume that I have 2 Lists of Strings. First contains "String A" and "String B", second one contains "String B" and "String A" (notice difference in order). I want to test them with JUnit to check whether they contains exactly the same Strings.

Is there any assert that checks equality of Strings that ignore order? For given example org.junit.Assert.assertEquals throws AssertionError

java.lang.AssertionError: expected:<[String A, String B]> but was:<[String B, String A]> 

Work around is to sort Lists firstly and then pass them to assertion. But I want my code to be as simple and clean as possible.

I use Hamcrest 1.3, JUnit 4.11, Mockito 1.9.5.

like image 316
kukis Avatar asked Apr 02 '14 09:04

kukis


People also ask

How do you compare two lists in assertEquals?

To compare two lists specifically, TestNG's Assert class has a method known as assertEquals(Object actual, Object expected) and there is an extended version of this method with customized message as assertEquals(Object actual, Object expected, String message). if the elements of the lists are in the same order.

How do you compare two lists irrespective orders in Java?

We can use the logic below to compare the equality of two lists using the assertTrue and assertFalse methods. In this first test, the size of both lists is compared before we check if the elements in both lists are the same. As both of these conditions return true, our test will pass.

How do you assert two lists in JUnit?

You can use assertEquals in junit. If the order of elements is different then it will return error. If you are asserting a model object list then you should override the equals method in the specific model.

What is the difference between assertEquals and assertSame?

assertEquals() Asserts that two objects are equal. assertSame() Asserts that two objects refer to the same object. the assertEquals should pass and assertSame should fail, as the value of both classes are equal but they have different reference location.


2 Answers

As you mention that you use Hamcrest,
So I would pick one of the collection Matchers

import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; import static org.junit.Assert.assertThat;  public class CompareListTest {      @Test     public void compareList() {         List<String> expected = Arrays.asList("String A", "String B");         List<String> actual = Arrays.asList("String B", "String A");                  assertThat("List equality without order",              actual, containsInAnyOrder(expected.toArray()));     }      } 
like image 66
cheffe Avatar answered Oct 19 '22 04:10

cheffe


You can use List.containsAll with JUnit's assertTrue to check that the first list contains every element from the second one, and vice versa.

assertEquals(expectedList.size(), actualList.size()); assertTrue(expectedList.containsAll(actualList)); assertTrue(actualList.containsAll(expectedList)); 

Hint:
This doesn't work with duplicates in the lists.

like image 28
robertoia Avatar answered Oct 19 '22 02:10

robertoia