GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `satisfiesExactlyInAnyOrder` fails if `actual` overrides `equals` · Issue #3339 · assertj/assertj

satisfiesExactlyInAnyOrder fails if actual overrides equals #3339

New issue
Copy link
New issue
Copy link
Closed
🐛 Bug
Closed
satisfiesExactlyInAnyOrder fails if actual overrides equals#3339
🐛 Bug
Copy link
Assignees
scordio
Milestone
3.25.2

Description

@PBoddington
PBoddington
opened on Jan 19, 2024
Issue body actions

equals and hashCode are used in the implementation of satisfiesExactlyInAnyOrder (when they definitely aren't needed). This can result in incorrect assertion results.

  • assertj core version: 3.25.1
  • java version: 17
  • test framework version: n/a
  • os (if relevant):

Example of the issue:

public class MyTest {

  @Test
  void shouldPassButFails() {
    StringAndInt first = new StringAndInt("X", 1);
    StringAndInt second = new StringAndInt("X", 2);
    assertThat(List.of(first, second)).satisfiesExactlyInAnyOrder(
        stringAndInt -> assertThat(stringAndInt.integer).isEqualTo(2),
        stringAndInt -> assertThat(stringAndInt.integer).isEqualTo(1)
    );
  }

  private static final class StringAndInt {
    private final String string;
    private final int integer;

    private StringAndInt(
        String string,
        int integer
    ) {
      this.string = string;
      this.integer = integer;
    }

    @Override
    public int hashCode() {
      return string.hashCode();
    }

    @Override
    public boolean equals(Object o) {
      return (o instanceof StringAndInt other) && other.string.equals(string);
    }

    @Override
    public String toString() {
      return "StringAndInt{" +
          "string='" + string + '\'' +
          ", integer=" + integer +
          '}';
    }
  }
}

This test fails with

Expecting actual:
  [StringAndInt{string='X', integer=1}, StringAndInt{string='X', integer=2}]
to satisfy all the consumers in any order.

but should pass.

The problem is that the use of List::remove in the implementation results in the wrong element being removed.

In general we believe there are a number of bugs in assertj due to unnecessary usages of equals and hashCode, and think that the best way to weed them out is to have unit tests using classes where equals and hashCode throw exceptions.

Reacted by David Boden and Joel Costigliola

Activity

  1. PBoddington
    changed the title [-]Bug in `satisfiesExactlyInAnyOrde`r due to usages of `equals` and `hashCode` in the implementation[/-] [+]Bug in `satisfiesExactlyInAnyOrder` due to usages of `equals` and `hashCode` in the implementation[/+] on Jan 19, 2024
  2. scordio commented on Jan 19, 2024

    @scordio
    scordio
    on Jan 19, 2024
    Member
    More actions

    Thank you for reporting it, @PBoddington!

    Reacted by Paul Boddington
  3. scordio
    added
    type: bug
    on Jan 19, 2024
  4. scordio
    added this to the 3.25.2 milestone on Jan 19, 2024
  5. scordio
    self-assigned this
    on Jan 19, 2024
  6. scordio
    changed the title [-]Bug in `satisfiesExactlyInAnyOrder` due to usages of `equals` and `hashCode` in the implementation[/-] [+]`satisfiesExactlyInAnyOrder` fails if `actual` overrides `equals`[/+] on Jan 21, 2024
  7. scordio
    closed this as completedin d51cfd9on Jan 21, 2024
  8. scordio commented on Jan 21, 2024

    @scordio
    scordio
    on Jan 21, 2024
    Member
    More actions

    In general we believe there are a number of bugs in assertj due to unnecessary usages of equals and hashCode, and think that the best way to weed them out is to have unit tests using classes where equals and hashCode throw exceptions.

    Do you have any specific examples in mind?

  9. PBoddington commented on Jan 22, 2024

    @PBoddington
    PBoddington
    on Jan 22, 2024
    Author
    More actions

    Thanks for fixing so quickly!

    In general we believe there are a number of bugs in assertj due to unnecessary usages of equals and hashCode, and think that the best way to weed them out is to have unit tests using classes where equals and hashCode throw exceptions.

    Do you have any specific examples in mind?

    One other thing was #3340 but it's hard to give other specific examples right now. This came about because as an experiment to see if people in our org were writing code relying on undocumented equals/hashCode behaviour of certain core classes, we changed the implementations to simply throw exceptions to see what tests failed. Many of the exceptions we saw were coming from assertj itself. The reason it's so difficult to say whether these are bugs or not is that it would not be a bug in itself for assertj to be calling equals/hashCode on the arguments passed. I'd have to go through the exceptions and find if equals/hashCode was being used correctly. In this particular case I was able to see quite quickly that it was a bug, but other exceptions require more analysis. I'll create more issues if I do find any more.

  10. scordio commented on Jan 22, 2024

    @scordio
    scordio
    on Jan 22, 2024
    Member
    More actions

    Thanks, Paul!

    @joel-costigliola is already looking at #3340. I couldn't spot any other obvious case where the same mistake happened but we'll keep an eye on it.

    Reacted by Paul Boddington
  11. schlosna
    added a commit that references this issue on Jan 26, 2024

    Remove elements by reference in `assertSatisfiesExactlyInAnyOrder`

    This issue will close once commit d51cfd9 is merged into the 'main' branch.
    d51cfd9
  12. scordio
    removed
    type: bug
    on Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @scordio
    scordio

Labels

No labels
No labels

Type

🐛 Bug

Projects

No projects

    Milestone

    • 3.25.2

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.