1

I want to test if some list of strings is generated correctly by creating unit tests using JUnit.

I have 2 lists of strings (one list in my code is private static final, let's say list1) with the same elements (the same elements can multiply) in a different order:

List<String> list1 = Arrays.asList("a","b","c");
List<String> list2 = Arrays.asList("c","a","b");
assertThat(list1 , containsInAnyOrder(list2));

This is not working and the junit test returns that the first element is not matching.

I am probably using the containsInAnyOrder method wrong.

containsInAnyOrder(java.util.Collection<Matcher<? super T>> itemMatchers) 

I don't know how to implement this Matcher.

I don't want to use this type of function as it is only ok for a small amount of elements:

containsInAnyOrder(T... items)
Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72

4 Answers4

4

You can first sort both the List and then compare the sorted Lists.

List<String> list1 = Arrays.asList("a","b","c");
List<String> list2 = Arrays.asList("c","a","b",);
Collections.sort(list1);
Collections.sort(list2);
assertEquals( list1, list2 ); // true
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
2

It depends if you expect to have duplication or not.

If there is no duplication 1 simple option is using Set - two Sets are equal to one another by using equals() and use assertEquals( setA, setB );

There are more "primitive" ways (using HashMap for each unique value and compare the repetition of each ) to do it but for what you are looking for check this answer

Mzf
  • 5,210
  • 2
  • 24
  • 37
0

I found this code in a tutorial about unit tests:

assertThat(list1, containsInAnyOrder("c", "b", "a"));    

Full method for testing lists:

@Test
public void testAssertList() {

    List<String> actual = Arrays.asList("a", "b", "c");
    List<String> expected = Arrays.asList("a", "b", "c");

    //All passed / true

    //1. Test equal.
    assertThat(actual, is(expected));

    //2. If List has this value?
    assertThat(actual, hasItems("b"));

    //3. Check List Size
    assertThat(actual, hasSize(3));

    assertThat(actual.size(), is(3));

    //4.  List order

    // Ensure Correct order
    assertThat(actual, contains("a", "b", "c"));

    // Can be any order
    assertThat(actual, containsInAnyOrder("c", "b", "a"));

    //5. check empty list
    assertThat(actual, not(IsEmptyCollection.empty()));

    assertThat(new ArrayList<>(), IsEmptyCollection.empty());

}

Your test is probably failing because list1 doesn't actually contains list2, but contains the elements in list2

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
0

So far, this is the most universal solution with no sets, additional sorting,...

assertThat(list1, containsInAnyOrder(list2.toArray()));

But I still want to find out how to implement this call:

containsInAnyOrder(java.util.Collection<Matcher<? super T>> itemMatchers)
  • Try `containsInAnyOrder(isIn(list2))` Will not handle duplicate entries though, since the `containsInAnyOrder` doesn't care about duplicates. – Sgene9 Oct 17 '17 at 21:20
  • 1
    The "InAnyOrder" is not meant for the elements of the list but for the matchers. It's saying that, given multiple matchers, each element can satisfy any of the matchers in any order. – Sgene9 Oct 17 '17 at 21:28