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

Abstract ValueObject class with EqualsBuilder and HashCodeBuilder #102

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

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
1. Have an abstract ValueObject class that defines equals and hashCode 
accordingly using EqualsBuilder and HashCodeBuilder:

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

public abstract class ValueObject {

    @Override
    public final boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public final int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}
2. And a simple subclass:

public class A extends ValueObject {

    private final int a;
    private final int b;

    public A(int a, int b) {
        this.a = a;
        this.b = b;
    }

    public int getA() {
        return this.a;
    }

    public int getB() {
        return this.b;
    }

}
3. Being tested with EqualsVerifier like this:

public class ATest {

    @Test
    public void equalsContract() throws Exception {
        EqualsVerifier.forClass(A.class)
            .withRedefinedSuperclass()
            .allFieldsShouldBeUsed()
            .verify();
    }

}


What is the code (equals method, hashCode method, relevant fields) that
triggers the problem?
Available in the first answer.


What error message does EqualsVerifier give?
java.lang.AssertionError: Significant fields: all fields should be used, but A 
has not defined an equals method.

This exception is not thrown if I don't set allFieldsShouldBeUsed, but then the 
test wouldn't guarantee that all fields are indeed being used.


What stacktrace does EqualsVerifier print, when called with the debug()
method?
java.lang.AssertionError: Significant fields: all fields should be used, but A 
has not defined an equals method.
For more information, go to: http://www.jqno.nl/equalsverifier/errormessages
    at nl.jqno.equalsverifier.EqualsVerifier.handleError(EqualsVerifier.java:378)
    at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:361)

What did you expect?
Passing test.


What version of EqualsVerifier are you using?
1.5.1

Please provide any additional information below.
See conversation in google group:
https://groups.google.com/forum/#!topic/equalsverifier/StWs2oybPAo


Original issue reported on code.google.com by andre.sc...@gmail.com on 8 Jan 2015 at 3:31

@GoogleCodeExporter
Copy link
Author

As discussed on the mailinglist as well.

Original comment by jan.ouw...@gmail.com on 9 Jan 2015 at 8:32

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

@GoogleCodeExporter
Copy link
Author

Fixed in version 1.6.

Original comment by jan.ouw...@gmail.com on 17 Jan 2015 at 4:27

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

@GoogleCodeExporter
Copy link
Author

Nice Job, Jan! Thank you!

Original comment by andre.sc...@gmail.com on 17 Jan 2015 at 8:03

  • 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