A subtlety of Assert Equals in TestNG

Alonso Del Arte
2 min readJul 4, 2024

--

Photo by Geranimo on Unsplash

To check that two arrays are equal in JUnit, you have to use assertArrayEquals(). TestNG doesn’t have assertArrayEquals(), so you just use assertEquals().

In TestNG, we see that both of the assertions shown pass.

    @Test
public void demoAssertEqualsOnArray() {
int[] arrayA = {1, 2, 3, 4, 5};
int[] arrayB = {1, 2, 3, 4, 5};
assert !arrayA.equals(arrayB)
: "Arrays not considered equal by equals()";
assertEquals(arrayA, arrayB);
}

And NetBeans gives me a warning about the first assertion, and offers to change it to Arrays.equals(). With JUnit, the second assertion would fail. The arrays are not equal by equals().

In the TestNG assertions library, assertEquals() is overloaded with versions that take actual and expected that are both arrays of integers, both arrays of floating point numbers, both arrays of objects, etc.

One of those overloads is for actual and expected that are both instances of java.util.Collection. That means that in TestNG, we can test that two different kinds of collections are equal if they contain the same elements. For example:

    @Test
public void demoAssertEqualsOnCollections() {
Set<String> collectionA = new HashSet<>();
collectionA.add("GHI");
collectionA.add("DEF");
collectionA.add("ABC");
List<String> collectionB = new ArrayList<>();
collectionB.add("ABC");
collectionB.add("DEF");
collectionB.add("GHI");
assert !collectionA.equals(collectionB)
: "Collections not considered equal by equals()";
assertEquals(collectionA, collectionB);
}

Remember that sets (except for instances of SortedSet) are unordered, so in this example the assertion will pass if the set and the list contain the same elements, regardless of what order they were added in.

Of course if both collections are ordered collections, such as both of them being lists, insertion order matters. In this next example, the second assertion fails:

    @Test
public void demoAssertEqualsOnDifferentOrderLists() {
List<String> collectionA = new LinkedList<>();
collectionA.add("GHI");
collectionA.add("DEF");
collectionA.add("ABC");
List<String> collectionB = new ArrayList<>();
collectionB.add("ABC");
collectionB.add("DEF");
collectionB.add("GHI");
assert !collectionA.equals(collectionB)
: "Collections not considered equal by equals()";
assertEquals(collectionA, collectionB);
}

On this one, TestNG reports that

Lists differ at element [0]: ABC != GHI expected [ABC] but found [GHI]

In this case, the TestNG documentation leaves something to be desired.

Asserts that two collections contain the same elements in the same order. If they do not, an AssertionError is thrown.

As I have shown, that is only the case if both collections are ordered collections.

For this and many other reasons, it’s important to always read test failure explanations, to make sure a test failed for the reason you expected.

--

--

Alonso Del Arte
Alonso Del Arte

Written by Alonso Del Arte

is a Java and Scala developer from Detroit, Michigan. AWS Cloud Practitioner Foundational certified

No responses yet