Learn different ways of asserting if two ArrayLists in Java have the same elements in the same order.
Tutorial Contents
Overview
A Java List is a dynamically growing ordered Collection of elements. Two Lists are equal if they contain the same elements in the same order.
In Java, especially in Unit Testing, we often must assert if a List contains the expected elements in a specific order. This tutorial quickly demonstrates how to compare two Java ArrayLists for equality using some of the most popular Unit Tests frameworks like JUnit, TestNG, and AssertJ.
For the tutorial, consider we have the following four ArrayList instances.
List<Integer> list1 = List.of(10, 11, 12, 14);
List<Integer> list2 = list1;
List<Integer> list3 = List.of(10, 11, 12, 14);
List<Integer> list4 = List.of(14, 12, 11, 10);
Code language: Java (java)
Please note that the first and second refer to the same ArrayList. The third List contains the same elements in the same order as that of the first List. The fourth List, however, has the same elements but in a different order.
Using TestNG
TestNG provides straightforward ways to assert if two ArrayList have the same elements in the same order.
Example of testing two Lists for equality in TestNG.
Assert.assertEquals(list1, list2);
Assert.assertSame(list1, list2);
Assert.assertEquals(list1, list3);
Assert.assertNotSame(list1, list3);
Assert.assertNotEquals(list1, list4);
Code language: Java (java)
Please note that the assertSame() method asserts that the given reference variables refer to the same Object. The assertEquals(), on the other hand, asserts equality.
Using JUnit
Interestingly, the JUnit way of asserting that two Lists are equal is the same as TestNG.
Example of checking if two Java Lists are equal in TestNG.
Assert.assertEquals(list1, list2);
Assert.assertSame(list1, list2);
Assert.assertEquals(list1, list3);
Assert.assertNotSame(list1, list3);
Assert.assertNotEquals(list1, list4);
Code language: Java (java)
Similar to TestNG, the JUnit assertSame() verifies if the given reference variables belong to a physically same Object. The assertEqual() method verifies if the given Objects are equal using their equals() method.
Using AssertJ
Let’s quickly understand how to compare two ArrayList in AssertJ.
Assertions.assertThat(list1)
.isEqualTo(list2)
.isSameAs(list2)
.isEqualTo(list3)
.isNotSameAs(list3)
.isNotEqualTo(list4);
Code language: Java (java)
Using Java Object Equality
As stated earlier, the equals() method in the List class verifies if two List instances have the same elements in the same order, and this is true across all the implementations of the List interface.
assert list1.equals(list2);
assert list1 == list2;
assert list1.equals(list3);
assert list1 != list3;
assert !list1.equals(list4);
Summary
This quick tutorial demonstrated ways of testing if two Java Lists are equal. Two List implementations are considered equal when they have the same elements in the same order.
We studied how to assert that the two Lists are the same or are equal using JUnit, TestNG, AssertJ, and plain Java Object equality.
Please refer to our GitHub Repository for the complete source code.
More List This:
- Comparing Two Lists In Java
- Finding the Differences Between Two Java Lists
- Asserting Java Lists for Equality Ignoring the Order
- How to Initialize a List Inline in Java