Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should test that sequential calls to hashcode return the same int when object has no state #97

Closed
GoogleCodeExporter opened this issue Mar 29, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?

Given this class 

public final class VariableHash {
    @Override
    public int hashCode() {
        return (int)(1000*Math.random());
    }
}

Test the class with this unit test

@Test
public void equalsContract() {
    EqualsVerifier.forClass(VariableHash.class).verify();
}

/////////////////////////////////////

What is the code (equals method, hashCode method, relevant fields) that
triggers the problem?

hashcode method in a class with no fields that doesn't also override equals

/////////////////////////////////////

What error message does EqualsVerifier give?

No error message, but the above test passes instead of fails.

This test actually does fail as one would expect:

@Test
public void testVariableHash() {
    Object c = new VariableHash();
    assertEquals(c.hashCode(), c.hashCode());
}

/////////////////////////////////////

What stacktrace does EqualsVerifier print, when called with the debug()
method?

N/A

/////////////////////////////////////

What did you expect?

Expected the test to fail.

/////////////////////////////////////

What version of EqualsVerifier are you using?

1.5

/////////////////////////////////////

Please provide any additional information below.

See conversation in google group:
https://groups.google.com/forum/?fromgroups#!topic/equalsverifier/mflja4nNBqw

Maybe we can add a check where EV tests the hashCode of an object on itself.

Maybe we should also add a check that if hashCode is defined, so should equals 
be (and vice versa). But it would be necessary that such a check can be 
disabled, too.



Original issue reported on code.google.com by [email protected] on 26 Sep 2014 at 1:30

@GoogleCodeExporter
Copy link
Author

Original comment by [email protected] on 27 Sep 2014 at 2:20

  • Changed state: Accepted
  • Added labels: ****
  • Removed labels: ****

@GoogleCodeExporter
Copy link
Author

Fixed in version 1.6.

Original comment by [email protected] on 17 Jan 2015 at 4:26

  • Changed state: Fixed
  • Added labels: ****
  • Removed labels: ****

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant