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

Equalsverifier fails when run with quarkus:dev #550

Closed
talip-ozkeles-kabisa opened this issue Dec 3, 2021 · 3 comments
Closed

Equalsverifier fails when run with quarkus:dev #550

talip-ozkeles-kabisa opened this issue Dec 3, 2021 · 3 comments

Comments

@talip-ozkeles-kabisa
Copy link

talip-ozkeles-kabisa commented Dec 3, 2021

Describe the bug
Equalsverifier fails when run with quarkus:dev

To Reproduce
When running equalsverfier in quarkus dev mode, equalsverfier tests fail.

I tried to test a class with equalsverifier. This works in my IDE. I tried to use it in quarkus dev mode (by running ./mvnw quarkus:dev), but then it fails with the following exception:

ERROR [io.qua.test] (Test runner thread) Test DingetjeTest#implementsEquals() failed
: java.lang.AssertionError: EqualsVerifier found a problem in class a.Dingetje.
-> Can not set final java.lang.String field a.Dingetje.text to a.Dingetje

For more information, go to: http://www.jqno.nl/equalsverifier/errormessages
at nl.jqno.equalsverifier.api.SingleTypeEqualsVerifierApi.verify(SingleTypeEqualsVerifierApi.java:308)
at a.DingetjeTest.implementsEquals(DingetjeTest.java:11)
Caused by: java.lang.IllegalArgumentException: Can not set final java.lang.String field a.Dingetje.text to a.Dingetje
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at java.base/jdk.internal.reflect.UnsafeQualifiedObjectFieldAccessorImpl.get(UnsafeQualifiedObjectFieldAccessorImpl.java:38)
at java.base/java.lang.reflect.Field.get(Field.java:418)
at nl.jqno.equalsverifier.internal.reflection.FieldModifier.lambda$copyTo$1(FieldModifier.java:79)
at nl.jqno.equalsverifier.internal.reflection.FieldModifier.lambda$change$3(FieldModifier.java:113)
at nl.jqno.equalsverifier.internal.util.Rethrow.lambda$rethrow$0(Rethrow.java:47)
at nl.jqno.equalsverifier.internal.util.Rethrow.rethrow(Rethrow.java:30)
at nl.jqno.equalsverifier.internal.util.Rethrow.rethrow(Rethrow.java:45)
at nl.jqno.equalsverifier.internal.util.Rethrow.rethrow(Rethrow.java:55)
at nl.jqno.equalsverifier.internal.reflection.FieldModifier.change(FieldModifier.java:113)
at nl.jqno.equalsverifier.internal.reflection.FieldModifier.copyTo(FieldModifier.java:79)
at nl.jqno.equalsverifier.internal.reflection.InPlaceObjectAccessor.copyInto(InPlaceObjectAccessor.java:43)
at nl.jqno.equalsverifier.internal.reflection.InPlaceObjectAccessor.copy(InPlaceObjectAccessor.java:24)
at nl.jqno.equalsverifier.internal.checkers.ExamplesChecker.checkSingle(ExamplesChecker.java:84)
at nl.jqno.equalsverifier.internal.checkers.ExamplesChecker.check(ExamplesChecker.java:47)
at nl.jqno.equalsverifier.api.SingleTypeEqualsVerifierApi.verifyWithExamples(SingleTypeEqualsVerifierApi.java:413)
at nl.jqno.equalsverifier.api.SingleTypeEqualsVerifierApi.performVerification(SingleTypeEqualsVerifierApi.java:369)
at nl.jqno.equalsverifier.api.SingleTypeEqualsVerifierApi.verify(SingleTypeEqualsVerifierApi.java:304)
... 1 more

Code that triggers the behavior
Here's the class under test:

package a;

import java.util.Objects;

public class Dingetje {
    private final String text;

    public Dingetje(String text) {
        this.text = text;
    }

    @Override
    public final boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Dingetje)) {
            return false;
        }
        Dingetje other = (Dingetje) o;
        return text.equals(other.text);
    }

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

And the test:

package a;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

class DingetjeTest {
    @Test
    void implementsEquals() {
        EqualsVerifier.forClass(Dingetje.class)
                .withNonnullFields("text")
                .verify();
    }
}

Version
3.7.2

Additional context
Self-contained Quarkus project made in Github repo and shared with jqno

@jqno
Copy link
Owner

jqno commented Dec 3, 2021

The problem had to do with the fact that every time the tests run again, they're using a new ClassLoader, while Objenesis caches classes it creates, so they keep the original ClassLoader. This causes mismatches, and the only way to fix it, is by throwing away the Objenesis instance and creating a new one. There was no way to do that.

However, I have a new version syncing with Maven Central right now (version 3.8), which adds a way to do that. If you write your tests like this, it should work again:

        EqualsVerifier.forClass(Dingetje.class)
                .withResetCaches()
                .withNonnullFields("text")
                .verify();

@jqno jqno closed this as completed Dec 3, 2021
@jqno
Copy link
Owner

jqno commented Dec 2, 2022

Hi! At the Brabant JUG meetup a few months ago, you mentioned this issue still occurs with abstract classes. This is now fixed in EqualsVerifier 3.12.1! 🥳
Hope this helps!

@talip-ozkeles-kabisa
Copy link
Author

Thanks Jan,
It works perfectly now!

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

No branches or pull requests

2 participants