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.

Problem should override equals and hashcode

See original GitHub issue

I would like to write tests to compare thrown problems like this exception.expect(is(Problem.valueOf(MoreStatus.UNPROCESSABLE_ENTITY, "Some error"))) org.hamcrest.core.Is compares objects using equals method of objects. So to be able write those kind of tests I need Problem to override equals method.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

1reaction
whiskeysierracommented, Jun 3, 2016

Custom problems can override those methods themselves.

No, that would break the contract of equals:

  1. It is reflexive: For any reference value x, x.equals(x) must return true.
  2. It is symmetric: For any reference values x and y, x.equals(y) must return true if and only if y.equals(x) return true.
  3. It is transitive. For any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) must return true.
  4. It is consistent. For any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified.

I don’t like implementing equals/hashCode unless it’s in a final class and there is an obvious implementation that everyone agrees about.

I agree. Doing this right is borderline impossible on interface level. I recommend reading Chapter 3 of Effective Java Programming Language Guide By Joshua Bloch on this.

0reactions
michaeltecourtcommented, Jan 5, 2018

@whiskeysierra Has this issue been abandoned ? How do you unit test without some sort of equals method or matcher ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why to Override equals(Object) and hashCode() method
Whenever it(hashcode) is invoked on the same object more than once during an execution of a Java application, the hashCode method must ...
Read more >
Why do we need to override equals and hashcode methods in ...
We know that two objects are considered equal only if their references point to the same object, and unless we override equals and...
Read more >
Why : Overriding equals() and hashCode() in Java - Medium
i added these emp objects to a HashSet. Set should override the duplicate values and should return me only one object as both...
Read more >
Why Override equals, hashcode and toString method in Java ...
According to which, you must override hashcode if you are overriding equals and vice-versa. The problem is that this is not enforced by...
Read more >
Java equals() and hashCode() Contracts - Baeldung
If we want to use instances of the Team class as HashMap keys, we have to override the hashCode() method so that it...
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