-
Notifications
You must be signed in to change notification settings - Fork 379
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
Add MethodUtil
to make hasProperty()
work for Java Records
#426
base: master
Are you sure you want to change the base?
Conversation
The current `hasProperty()` matcher can't deal with Java Records, as they are not compatible with JavaBeans specification. In this change, `HasProperty` first tries to do the original thing and then tries to find a method of which name is equivalent to the given property name, if there's no matching property. So for example, if the class has a getter method named `property()`, `hasProperty()` would regard it as the class has a property named `property`. I hope this change might be properly and easily removed when the time comes and Hamcrest starts to support Java 17.
There is a flaw in 8201577, which is that the logic can't distinguish if the found method is a getter. Let's put a simple additional rule: The property must be a getter, so it should return something.
I've had a closer look. There are a few points I'd like to see addressed. Firstly, tests looked good and worked fine for the import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.core.IsEqual.equalTo;
record Example(String strAttribute, int intAttribute) {}
public class RecordPropertyTest {
@Test
public void
testExampleRecord() {
Example example = new Example("one", 1);
assertThat(example, hasProperty("strAttribute"));
assertThat(example, hasProperty("intAttribute"));
assertThat(example, hasProperty("strAttribute", equalTo("one")));
assertThat(example, hasProperty("intAttribute", equalTo(1)));
}
} The above code worked fine for the first 2 assertions, but failed for the last 2 that tried to match the the value. Looking at the changes, this is expected. I do think that we should offer a complete solution when we merge this PR, though. Having said that, I think it's understandable to check the approach first. Looking at What do you think? |
Thank you for the detailed feedback, @tumbarumba!
To summarize, I'd like to suggest that we focus on Thank you. |
Thanks for your comments @djkeh. I'm not keen to merge the PR as is. I think many people will find it confusing as to why one of the As to |
I got your point, @tumbarumba I agree with you. I'll respond in detail when I get back home. |
Thanks for the detailed feed back, @tumbarumba
|
Resolves #392
The current
hasProperty()
matcher can't deal with Java Records, as they are not compatible with JavaBeans specification.In this change,
HasProperty
tries the original thing first,and if there's no matching property, tries to find a method of which name is equivalent to the given property name.
So for example, if the class has a method named
property()
,hasProperty()
would regard it as the class has a property namedproperty
.This change is locally tested on JDK 17 with
record
.I deliberately created
MethodUtil
for the future change. If Hamcrest starts to support Java 17,the current approach can be polished using modern features such as
java.lang.Class.isRecord()
.I hope this change might be properly removed when the time comes and Hamcrest starts to support Java 17.
If merged, I will look into the other relevant matchers like
HasPropertyWithValue
based on this change.Reference