-
Notifications
You must be signed in to change notification settings - Fork 75
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
Exception thrown in record compact constructor causes test failure #613
Comments
Note that this is also an issue when testing a class which uses a record as a field (which needs to be instantiated by EqualsVerifier). Here, using |
Unfortunately, records in Java are well protected from reflection. (Actually, that's a good thing, but it makes things for EqualsVerifier a little more difficult.) We can instantiate classes via reflection without calling the constructor, but for records, the constructor must be called, no exceptions. So at the very least, if you want to check invariants like you do in your example, we should add some prefab values, like this: EqualsVerifier.forClass(Foo.class)
.withPrefabValues(int.class, 3, 4)
.verify(); Unfortunately, EqualsVerifier still has some tests where it plugs default values (i.e., EqualsVerifier.forClass(Foo.class)
.withPrefabValues(int.class, 3, 4)
.suppress(Warning.DEFAULT_VALUES)
.verify(); Where I agree that this is a desirable thing for EqualsVerifier, so I will try to impement it soon, if I can find some spare time. But it will never look as nice as when using regular classes, unfortunately. |
Thanks! It might suffice to change the error messages for exceptions thrown by some record constructor, and hint at the prefab values trick. I'm quite happy with this workaround, as I already have test fixtures. Plugging in |
I think I have a solution in PR #614 . Your test would then look like this: Lines 51 to 55 in b4f4a7a
I can't merge the PR yet because I need to write some documentation for it. |
I've just released version 3.10 with this fix. There's also some documentation at https://jqno.nl/equalsverifier/errormessages/record-failed-to-invoke-constructor/ |
A compact-constructor asserting non-null objects also triggers this. However I was initially quite confused by the (updated) error message & advice, as I could not find any numeric zeros around. (I think also "Record: failed to invoke constructor" gave the impression of some kind of internal limitation before invoking the constructor - it was successfully invoked, it just didn't complete.) However after a (rubbish) night's sleep, I've now joined the dots. I imagine use of
Should the error message be extended to include null, and the documentation updated with a further remedy?
Hopefully these 2 examples would then be enough to help the developer extrapolate any further edge cases. Although I guess even better if it could detect NPE (which |
That's a good suggestion, I'll reopen this ticket and pick it up for a next version. I can't promise a timeframe though, as I don't have a lot of free time to work on EqualsVerifier these days. |
I've just released version 3.10.1 which has better error reporting for exceptions in record constructors. |
Hi, I've just returned from extended holiday & taken a glance. New documentation page looks excellent, nicely done! I noticed the link added to the list of error messages is broken, though. (Hopefully you'll have seen my comment in the source. It was my first code-review style comment via github.) |
Thanks for noticing, I've fixed the link! |
Describe the bug
Exceptions thrown in the compact constructor of a record causes EqualsVerifier to fail the test.
To Reproduce
In my test:
EqualsVerifier.forClass(Foo.class).suppress(Warning.NULL_FIELDS).verify();
The compact constructor is the result of converting a regular Java class:
(private final field, getter, generated equals/hashCode)
Error message
Expected behavior
I expect EqualsVerifier to pass the test. In general, I expect the same behaviour for classes and equivalent records.
Version
3.9.1
Additional context
Java 17
The text was updated successfully, but these errors were encountered: