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

HasProperty Matcher doesn't work with Java Records #392

Open
Tracked by #418
rozagerardo opened this issue Oct 26, 2022 · 4 comments
Open
Tracked by #418

HasProperty Matcher doesn't work with Java Records #392

rozagerardo opened this issue Oct 26, 2022 · 4 comments

Comments

@rozagerardo
Copy link

rozagerardo commented Oct 26, 2022

Describe the bug
I have the following simple assertion in my test:

hasProperty("name")

For the following record:

public record AnyDto(
    Long id,
    
    @NotBlank
    String name,
    
    String description) { }

And even though I can see the object does have the property (set with a value), the test fails.

I have tracked this down, and it seems the "issue" is generated in the PropertyUtil class, in the propertyDescriptorsFor method:

return Introspector.getBeanInfo(fromObj.getClass(), stopClass).getPropertyDescriptors();

This is what Introspector.getBeanInfo(fromObj.getClass(), stopClass) retrieves for a Record class:

Screen Shot 2022-10-26 at 15 17 30

If I change the AnyDto to a regular class, the test passes, because the Introspector method above does contain PropertyDescriptors for the class:

Screen Shot 2022-10-26 at 15 21 48

Note: the Record does contain name as a MethodDescriptor, maybe we can rely on these for the Records?

This is a related Stackoverflow Question:
https://stackoverflow.com/questions/66982522/how-can-i-assert-hasproperty-with-a-java-record

@CC007
Copy link

CC007 commented Jan 24, 2023

It would be nice if property descriptors would be supplemented with Class::getRecordComponents in the case that Class::isRecord is true

alecharp added a commit to alecharp/plugin-health-scoring that referenced this issue Apr 4, 2023
Test still failing because I'm using Java record and hamcrest/JavaHamcrest#392
@djkeh
Copy link

djkeh commented Aug 3, 2024

Is this still on the go? I strongly hope to see this happening. More and more people start using records for handler methods in spring boot.

@tumbarumba tumbarumba mentioned this issue Aug 3, 2024
9 tasks
@tumbarumba
Copy link
Member

@djkeh yes, I'm looking to do something to fix this in the next release.

@tumbarumba tumbarumba mentioned this issue Aug 6, 2024
2 tasks
@tumbarumba
Copy link
Member

I had a closer look at this issue. I don't think I can put a fix in for it right now. I'd like to keep the v3.x versions all compatible with Java 1.8. From v4.x, I will look to introduce features for more modern JVMs and runtime libraries.

Unfortunately, I don't have a timeline for how long it will take. I think there is a prerequisite for implementing JEP 238 support, which should give the forward/backward compatibility guarantees I think Hamcrest should support.

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

4 participants