Hi all,
Today I came across a strange behaviour of TestNG Assert.assertEquals() method when comparing Sets for equality. As you know Sets are collections which guarantee uniqueness but not ordering of elements. Therefore building two Sets with same element values, say 1 2 3 4 5, could lead to the following content:
Set1: [1,3,2,4,5]
Set2: [3,1,5,2,4]
Javadoc contract for Set equality does not consider the order of elements as being important; what counts is that elements inside the two collections being compared have the same values according to the object equals method. Therefore according to Java Set equality contract the two Sets above are equal.
However if using TestNG Assert.assertEquals(Set1, Set2), the two sets are not the same. This seems a documented bug which, scarily enough, has been marked as fixed but evidently not remediated. So if TestNG folks are not going to do anything about it, don't use TestNG Assert.assertEquals() to test Set equality.
Happy technology to everyone.
M.
Recent Comments