AdBlock Detected

It looks like you're using an ad-blocker!

Our team work realy hard to produce quality content on this website and we noticed you have ad-blocking enabled. Advertisements and advertising enable us to continue working and provide high-quality content.

Assert on lists ignoring the order

When performing tests in our applications, we often want to compare lists to ensure they are equal. However, sometimes the order is different and the test fails. In this post, we will see how we can perform an assert on lists ignoring the order when only the content matters.

Introducción

When comparing lists using the equals method, we find that the lists must have the same elements in the same order to return true. Therefore, the equals method can only be used when the order is not important.

For example, if we run the following test, we will get false even though our lists have the same elements:

  @Test
  public void given_two_list_when_assert_then_return_false() {
    List listA = Arrays.asList("Amarillo", "Rojo", "Azul", "Verde", "Gris");
    List listB = Arrays.asList("Rojo", "Azul", "Verde", "Gris","Amarillo");

    assertFalse(listA.equals(listB));

  }

Next, we will look at different approaches to verify if two lists are equal.

Using Apache Commons to check if two lists are equal regardless of the order

Although the most common library for testing in our application is JUnit, Apache Commons provides us with Apache CollectionUtils, which allows us to perform comparisons.

To do this, first add the Maven dependency:

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

Once we have the dependency, we can use CollectionUtils.isEqualCollection to compare lists regardless of the order.

  @Test
  public void given_two_list_when_assert_with_apache_commons_then_return_true() {
    List listA = Arrays.asList("Amarillo", "Rojo", "Azul", "Verde", "Gris");
    List listB = Arrays.asList("Rojo", "Azul", "Verde", "Gris","Amarillo");

    assertTrue(CollectionUtils.isEqualCollection(listA, listB));
  }

Assert on lists ignoring the order with JUnit

JUnit is the standard library for testing in our applications and for verifying results using assertions. JUnit does not provide a direct method like Apache Commons’ CollectionUtils, but we can use the containsAll method in Java.

  @Test
  public void given_two_list_when_assert_with_junit_then_return_true() {
    List listA = Arrays.asList("Amarillo", "Rojo", "Azul", "Verde", "Gris");
    List listB = Arrays.asList("Rojo", "Azul", "Verde", "Gris", "Amarillo");

    assertTrue(
        listA.size() == listB.size() && listA.containsAll(listB) && listB.containsAll(listA));
  }

Using Hamcrest for asserting on lists ignoring the order

Another option for testing in our applications is to use Hamcrest. First, add the Hamcrest dependency to your application’s pom.xml file:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <version>2.2</version>
    <scope>test</scope>
</dependency>

Once we have added the dependency to our application, we can use the containsInAnyOrder method from Hamcrest Matchers:

    @Test
    public void given_two_list_when_assert_with_hamcrest_then_return_true() {
      List listA = Arrays.asList("Amarillo", "Rojo", "Azul", "Verde", "Gris");
      List listB = Arrays.asList("Rojo", "Azul", "Verde", "Gris", "Amarillo");
      assertThat(listA, Matchers.containsInAnyOrder(listB.toArray()));
    }

Assert in testing with AssertJ

Finally, if we use AssertJ in our tests to perform assertions in Java, we can use the hasSameElementsAs method to check if we have the same elements.

The dependency would be:

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

Once we have added the dependency, we can use hasSameElementsAs:

  @Test
  public void given_two_list_when_assert_with_assertj_then_return_true() {
    List listA = Arrays.asList("Amarillo", "Rojo", "Azul", "Verde", "Gris");
    List listB = Arrays.asList("Rojo", "Azul", "Verde", "Gris", "Amarillo");
    assertThat(listA).hasSameElementsAs(listB);
  }

Conclusion

In this post, we have seen how to perform assert on lists ignoring the order, which can be very useful in our tests.

If you need more information, you can leave us a comment or send an email to refactorizando.web@gmail.com You can also contact us through our social media channels on Facebook or twitter and we will be happy to assist you!!

Leave a Reply

Your email address will not be published. Required fields are marked *