Master the most popular testing framework for Java, through the Learn JUnit course:
Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.
Get started with mocking and improve your application tests using our Mockito guide:
Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.
Get started with understanding multi-threaded applications with our Java Concurrency guide:
Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:
Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
But these can also be overused and fall into some common pitfalls.
To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:
Get started with Spring and Spring Boot, through the Learn Spring course:
>> LEARN SPRINGExplore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:
Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.
I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.
You can explore the course here:
Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.
Get started with Spring Data JPA through the guided reference course:
Refactor Java code safely β and automatically β with OpenRewrite.
Refactoring big codebases by hand is slow, risky, and easy to put off. Thatβs where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.
Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions β one for newcomers and one for experienced users. Youβll see how recipes work, how to apply them across projects, and how to modernize code with confidence.
Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.
1. Overview
Sometimes when writing unit tests, we need to make order-agnostic comparisons 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 make order-agnostic comparisons. Letβs take a look at them one by one.
3. Using JUnit
JUnit is a well-known 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 open-source, community-driven library 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 instances of the same element and the 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 overcome issues related to hasSameElementsAs(), we can use containsExactlyInAnyOrderElementsOf(), which verifies that both lists contain exactly the same elements and nothing else in any order:
assertThat(a).containsExactlyInAnyOrderElementsOf(b);
The test 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.5.0-M2</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.
