assertEquals doesn't work for sets
See original GitHub issueTestNG Version
7.1.0
Expected behavior
assertEquals
yields true if called with two sets that contain the same elements, even if they are defined as Collection
.
Actual behavior
Depending on the type of set and the type of objects they contain assertEquals
might work well or fail because it relies on iterators to compare the two sets and in the case of sets the iteration order is not guaranteed. This behavior is is most evident on LinkedHashSet
s but also appears on HashSet
s depending on the type of objects they contain. I suspect it works well with TreeSet
s, which have a predictable iteration order.
Is the issue reproductible on runner?
- Shell
- Maven
- Gradle
- Ant
- Eclipse
- IntelliJ
- NetBeans
Test case sample
Test case excerpt (full test in attachment):
@Test
public void assertEqualsWorksOnLinkedHashSets() {
final Collection<Integer> oneSet = new LinkedHashSet<>();
oneSet.add(1);
oneSet.add(2);
final Collection<Integer> anotherSet = new LinkedHashSet<>();
anotherSet.add(2);
anotherSet.add(1);
assertTrue(oneSet.equals(anotherSet)); //passes
assertEquals(oneSet, anotherSet); //fails
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:9
Top Results From Across the Web
JUnit 4 compare Sets - java - Stack Overflow
You can assert that the two Set s are equal to one another, which invokes the Set equals() method. public class SimpleTest {...
Read more >assertEquals(Set, Set) - Google Groups
Hi! The assertEquals(Set, Set) doesn't behave as Set.equals(set). Is this intentional? Calling assertEqual(...) with two Sets as arguments "hits" the
Read more >JUnit Assertions: assertEquals And asssertSame With Examples
Example 1: We will see how the assertEquals() work on the 2 string objects with the same values. We already know, that assertEquals() ......
Read more >unittest — Unit testing framework — Python 3.11.1 ...
The unittest module provides a rich set of tools for constructing and ... These methods are used instead of the assert statement so...
Read more >Assertion method Assert.assertEquals() example. - Java2Novice
Assert class provides a set of assertion methods useful for writing tests. Assert.assertEquals() methods checks that the two objects are equals or not....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I believe the contract of assertEquals is broken as long as it doesn’t return the same result as equals. And I have just thought of a simpler, cleaner solution than the one I proposed initially:
I’ll check out the project and try to create a pull request if I have some spare time in the following days.
@mchintoanu - You are using an instance of LinkedHashSet, which means you are expecting that order should be maintained.
assertEquals
method with Collection argument expects that order also should be same. If you don’t wont to maintain the order then useHashSet
instead ofLinkedHashSet
. Same method will work for you.