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

Wrapped enum with single value throws AssertionError #203

Closed
DavidLyhedDanielsson opened this issue Aug 1, 2018 · 2 comments
Closed

Wrapped enum with single value throws AssertionError #203

DavidLyhedDanielsson opened this issue Aug 1, 2018 · 2 comments
Labels

Comments

@DavidLyhedDanielsson
Copy link

What steps will reproduce the problem?

Running the given JUnit test

What is the code that triggers this problem?

public class DummyClass {
    enum DummyEnum {
        SINGLE_VALUE
    }

    @Test
    public void verifyHashCodeAndEquals() {
        class DummyInner {
            final DummyEnum value;

            public DummyInner(DummyEnum value) {
                this.value = value;
            }

            @Override
            public boolean equals(Object o) {
                if (this == o) {
                    return true;
                }
                if (o == null || getClass() != o.getClass()) {
                    return false;
                }
                DummyInner that = (DummyInner) o;
                return value == that.value;
            }

            @Override
            public int hashCode() {
                return Objects.hash(value);
            }
        }

        class DummyVersion {
            final String someData;
            final int someMoreData;
            final DummyInner dummyInner;

            public DummyVersion(String someData, int someMoreData, DummyInner dummyInner) {
                this.someData = someData;
                this.someMoreData = someMoreData;
                this.dummyInner = dummyInner;
            }

            @Override
            public boolean equals(Object o) {
                if (this == o) {
                    return true;
                }
                if (o == null || getClass() != o.getClass()) {
                    return false;
                }
                DummyVersion that = (DummyVersion) o;
                return someMoreData == that.someMoreData &&
                        Objects.equals(someData, that.someData) &&
                        Objects.equals(dummyInner, that.dummyInner);
            }

            @Override
            public int hashCode() {
                return Objects.hash(someData, someMoreData, dummyInner);
            }
        }

        EqualsVerifier.forClass(DummyVersion.class).usingGetClass().verify();
    }
}

What error message or stack trace does EqualsVerifier give?

java.lang.AssertionError: Significant fields: equals does not use dummyInner, or it is stateless.
For more information, go to: http://www.jqno.nl/equalsverifier/errormessages

    at nl.jqno.equalsverifier.EqualsVerifier.handleError(EqualsVerifier.java:389)
    at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:375)

What did you expect?

I expect the test to pass

Which version of EqualsVerifier are you using?

2.5

Please provide any additional information below.

The reason why I expect this to work is because even though the enum's value can only be SINGLE_VALUE right now, there will be more enums in the future. In reality, DummyVersion has a bunch of members where I do need to make sure equals and hashcode are correctly implemented, so I can't just skip using EqualsVerifier either.

I figured I would've been able to fix this by ignoring dummyInner:

EqualsVerifier
    .forClass(DummyVersion.class)
    .usingGetClass()
    .withIgnoredFields("dummyInner")
    .verify();

But that gives me

java.lang.AssertionError: Significant fields: equals should not use dummyInner, but it does.
For more information, go to: http://www.jqno.nl/equalsverifier/errormessages

    at nl.jqno.equalsverifier.EqualsVerifier.handleError(EqualsVerifier.java:375)
    at nl.jqno.equalsverifier.EqualsVerifier.verify(EqualsVerifier.java:361)
@jqno
Copy link
Owner

jqno commented Aug 2, 2018

Indeed, nice catch. I'll try to find a fix for this, and I'll get back to you.

@jqno jqno added the accepted label Aug 2, 2018
jqno added a commit that referenced this issue Aug 5, 2018
@jqno
Copy link
Owner

jqno commented Aug 5, 2018

I just released version 2.5.1, which fixes this.

@jqno jqno closed this as completed Aug 5, 2018
akhalikov pushed a commit to akhalikov/equalsverifier that referenced this issue Nov 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants