assertj: Enum comparison with String is not working with recursive assertion

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

public class TestEnumWithString {

    @Test
    public void testEnumWithString() {
        assertThat(Vehicle.CAR).usingRecursiveComparison().isEqualTo("CAR");
    }
}

enum Vehicle {
    CAR
}

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (15 by maintainers)

Commits related to this issue

Most upvoted comments

@NewbieProger this will be available in 3.25.0.

I’m almost done but thanks for the offer @armandino !

actually we will stick with withEnumStringComparison which reflects more that we are going to compare an enum to a string.

We will add comparingEnumsAsString, this addresses the enum case, the other ones can be with withEqualsForFieldsMatchingRegexes if given the proper regex.

I see one issue if we go that way, it won’t be clear what the scope of the String-comparison is. An alternative could be to add something like comparingByToString(Class... types) where one could specify the list of types to compare with toString(), need discussing with the team to see how they feel about it.

@joel-costigliola - I’m using latest version 3.22.0.