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

Relation between two values in the constructor #747

Open
mazurkin opened this issue Dec 21, 2022 · 2 comments
Open

Relation between two values in the constructor #747

mazurkin opened this issue Dec 21, 2022 · 2 comments

Comments

@mazurkin
Copy link

Simple test of the simple record

    record MyRecord(int total, int used) {

        MyRecord(int total, int used) {
            Preconditions.checkArgument(total >= 0);
            this.total = total;

            Preconditions.checkArgument(used >= 0);
            Preconditions.checkArgument(used <= total);
            this.used = used;
        }
    }

    @Test
    public void testMyRecord() {
        EqualsVerifier
            .forClass(MyRecord.class)
            .verify();
    }
java.lang.AssertionError: EqualsVerifier found a problem in class Test$MyRecord.
-> Record: failed to run constructor for record type Test$MyRecord
  These were the values passed to the constructor: [0, 1]
  If the record does not accept 0 or false as a value for its constructor parameters, consider suppressing Warning.ZERO_FIELDS.
  If the record does not accept any of the given values for its constructor parameters, consider providing prefab values for the types of those fields.

For more information, go to: https://www.jqno.nl/equalsverifier/errormessages
(EqualsVerifier 3.12.3, JDK 17.0.5 on Linux)

Supression suppress(Warning.ZERO_FIELDS) doesn't help as it fails with:

These were the values passed to the constructor: [1, 2]

Probably worth to create withPrefabValues() which takes a name of the constructor's field.

@mazurkin
Copy link
Author

also more complicated bean:

  record MyRecord(int total, Collection<String> samples) {

        MyRecord(int total, int used) {
            Preconditions.checkArgument(total >= 0);
            this.total = total;

            Preconditions.checkNonNull(samples)
            Preconditions.checkArgument(samples.size() <= total);
            this.samples = samples;
        }
    }

@jqno
Copy link
Owner

jqno commented Dec 23, 2022

Thanks for reporting this, this is indeed a problem that I don't have a solution for at the moment...

Your suggestion for adding a withPrefabValues overload which also takes the names of the fields is interesting, but I'm not sure if it's the best solution...I'll have to think about this a little.

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