Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 3525

Assert Two Lists for Equality Ignoring Order in Java

$
0
0

1. Overview

Sometimes when writing unit tests, we need to make order agnostic comparison of lists. In this short tutorial, we'll take a look at different examples of how we can write such unit tests.

2. Setup

As per the List#equals Java documentation, two lists are equal if they contain the same elements in the same order. Therefore we can't merely use the equals method as we want to do order agnostic comparison.

Throughout this tutorial, we'll use these three lists as example inputs for our tests:

List first = Arrays.asList(1, 3, 4, 6, 8);
List second = Arrays.asList(8, 1, 6, 3, 4);
List third = Arrays.asList(1, 3, 3, 6, 6);

There are different ways to do order agnostic comparison. Let's take a look at them one by one.

3. Using JUnit

JUnit is a well-know framework used for unit testing in the Java ecosystem.

We can use the logic below to compare the equality of two lists using the assertTrue and assertFalse methods.

Here we check the size of both lists and check if the first list contains all elements of the second list and vice versa. Although this solution works, it's not very readable. So now let's look at some alternatives:

@Test
public void whenTestingForOrderAgnosticEquality_ShouldBeTrue() {
    assertTrue(first.size() == second.size() && first.containsAll(second) && second.containsAll(first));
}

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.

Let's now take a look at a failing test:

@Test
public void whenTestingForOrderAgnosticEquality_ShouldBeFalse() {
    assertFalse(first.size() == third.size() && first.containsAll(third) && third.containsAll(first));
}

Contrastingly, in this version of the test, although the size of both lists is the same, all elements don't match.

4. Using AssertJ

AssertJ is an opensource community-driven library used for writing fluent and rich assertions in Java tests.

To use it in our maven project, let's add the assertj-core dependency in the pom.xml file:

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.16.1</version>
</dependency>

Let's write a test to compare the equality of two list instance of the same element and same size:

@Test
void whenTestingForOrderAgnosticEqualityBothList_ShouldBeEqual() {
    assertThat(first).hasSameElementsAs(second);
}

In this example, we verify first contains all the elements of the given iterable and nothing else, in any order. The main limitation of this approach is the hasSameElementsAs method ignores duplicates.

Let's look at this in practice to see what we mean:

@Test
void whenTestingForOrderAgnosticEqualityBothList_ShouldNotBeEqual() {
    List a = Arrays.asList("a", "a", "b", "c");
    List b = Arrays.asList("a", "b", "c");
    assertThat(a).hasSameElementsAs(b);
}

In this test, although we have the same elements, the size of both lists is not equal, but the assertion will still be true, as it ignores the duplicates. To make it work we need to add a size check for both lists:

assertThat(a).hasSize(b.size()).hasSameElementsAs(b);

Adding a check for the size of both our lists followed by the method hasSameElementsAs will indeed fail as expected.

5. Using Hamcrest

If we are already using Hamcrest or want to use it for writing unit tests, here is how we can use the Matchers#containsInAnyOrder method for order agnostic comparison.

To use Hamcrest in our maven project, let's add the hamcrest-all dependency in pom.xml file:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <version>1.3</version>
</dependency>

Let's look at the test:

@Test
public void whenTestingForOrderAgnosticEquality_ShouldBeEqual() {
    assertThat(first, Matchers.containsInAnyOrder(second.toArray()));
}

Here the method containsInAnyOrder creates an order agnostic matcher for Iterables, which does matching with examined Iterable elements. This test matches the elements of two lists, ignoring the order of elements in the list.

Thankfully this solution doesn't suffer from the same problem as explained in the previous section, so we don't need to compare the sizes explicitly.

6. Using Apache Commons

Another library or framework apart from JUnit, Hamcrest, or AssertJ, we can use is Apache CollectionUtils. It provides utility methods for common operations that cover a wide range of use cases and helps us avoid writing boilerplate code.

To use it in our maven project, let's add the commons-collections4 dependency in pom.xml file:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>

Here is a test using CollectionUtils:

@Test
public void whenTestingForOrderAgnosticEquality_ShouldBeTrueIfEqualOtherwiseFalse() {
    assertTrue(CollectionUtils.isEqualCollection(first, second));
    assertFalse(CollectionUtils.isEqualCollection(first, third));
}

The isEqualCollection method returns true if the given collections contain precisely the same elements with the same cardinalities. Otherwise, it returns false.

7. Conclusion

In this article, we have explored how to check the equality of two List instances, where the elements of both lists are ordered differently.

All these examples can be found over on GitHub.

The post Assert Two Lists for Equality Ignoring Order in Java first appeared on Baeldung.

        

Viewing all articles
Browse latest Browse all 3525

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>