question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

assertEquals doesn't work for sets

See original GitHub issue

TestNG 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 LinkedHashSets but also appears on HashSets depending on the type of objects they contain. I suspect it works well with TreeSets, 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
    }

AssertEqualsOnSetsTest.zip

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
mchintoanucommented, May 26, 2020

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:

    if (actual.equals(expected)) {
      return;
    }
    //rest of assertEquals(Collection, Collection, String) implementation

I’ll check out the project and try to create a pull request if I have some spare time in the following days.

1reaction
amitbhoraniyacommented, May 26, 2020

@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 use HashSet instead of LinkedHashSet . Same method will work for you.

final Collection<Integer> oneSet = new HashSet<>();
oneSet.add(1);
oneSet.add(2);
final Collection<Integer> anotherSet = new HashSet<>();
anotherSet.add(2);
anotherSet.add(1);
Assert.assertTrue(oneSet.equals(anotherSet)); //passes
Assert.assertEquals(oneSet, anotherSet); //passes
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found