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
Enum comparison with String is not working with recursive assertion #2616
Comments
@vimalbera92, thanks for reporting this. Which version are you using? |
@joel-costigliola - I'm using latest version 3.22.0. |
The behavior is as expected, We are discussing with the team the pros and cons of adding an option that would allow to compare enum with string. In the meantime you can register comparators per field or type to tweak the behavior as you want, see https://assertj.github.io/doc/#assertj-core-recursive-comparison-comparators. |
@joel-costigliola Thanks for quick response and workaround. I think it's not a good idea to include all primitive types to check against string but definitely worth enum comparison with string but I'll leave that to assertj team to decide and agree upon. |
Closing this issue as we can register specific comparison/equals for types or fields which I think cover the use case. |
@joel-costigliola, apologies for resurrecting an old issue. I wanted to add another vote for supporting enum/string comparison as it's a fairly common use case. Currently this requires assertThat(entity)
.usingRecursiveComparison()
.withEqualsForFields((String s, PhoneType t) -> PhoneType.valueOf(s) == t, "phones.phoneType")
.withEqualsForFields((String s, AddressType t) -> AddressType.valueOf(s) == t, "addresses.addressType")
// etc..
.isEqualTo(dto); It would be nice if this was supported OOTB via an option, e.g.: assertThat(entity)
.usingRecursiveComparison()
.withEnumStringComparison()
.isEqualTo(dto); |
What about |
What about a name like assertThat(entity).usingRecursiveComparison()
.comparingEnumsByName()
.isEqualTo(dto); |
What do you think about including other types, in addition to enums? e.g. class Person {
UUID id;
int age;
Gender gender;
boolean isActive;
}
class PersonDto {
String id;
String age;
String gender;
String isActive;
} I believe it's a pretty common use case where one side of the comparison has String representations of certain types. Including such an option would make |
The challenge here would be that the root objects, Note that you can actually achieve comparing all fields by string using PersonEntity person = new PersonEntity();
person.id = UUID.randomUUID();
person.age = 20;
person.isActive = true;
PersonDto personDto = new PersonDto();
personDto.id = person.id.toString();
personDto.age = "20";
personDto.isActive = "true";
assertThat(person).usingRecursiveComparison()
.withEqualsForFieldsMatchingRegexes((o1, o2) -> o1.toString().equals(o2.toString()), ".+")
.isEqualTo(personDto); This means that any objects that are not root will be compared by string, note the regex requires the field not to be empty (empty field = root object). |
@joel-costigliola I haven't used Do you see any issues if the String-comparison option was limited to enums and primitive/wrapper types? |
I see one issue if we go that way, it won't be clear what the scope of the String-comparison is. |
We will add |
actually we will stick with |
@joel-costigliola sounds good. I see it's assigned to you, but if help is needed with a PR, I can give it a try. |
I'm almost done but thanks for the offer @armandino ! |
Where did this method goes? |
@NewbieProger this will be available in 3.25.0. |
Summary
Comparison of enum with string fails with recursive assertion and
strictTypeChecking
as false. I would expect below code to be run successfully.actual and expected objects and their fields were compared field by field recursively even if they were not of the same type
- as per error, actual and expected values are same (CAR in below example) and test case should pass.Example
The text was updated successfully, but these errors were encountered: