withEqualsForType() where type is the type of a property in the expected object

68 Views Asked by At

I'm trying to compare two different types, DTO and Entity. DTO has:

private String id;
private String teamId;
private String conferenceId;

Entity has:

private UUID id;
private UUID teamId;
private UUID conferenceId;

In my test I have:

DTO actual = mapper.entityToDto(entity);

assertThat(actual)
  .usingRecursiveComparison()
  .isEqualTO(entity);

I'd like to use withEqualsForType((stringId, uuidId) -> comparison, UUID.class) but that doesn't work as I get an error about no method for signature UUID.class, UUID.class.

I also tried withEqualsForType((stringId, uuidID) -> comparison, String.class) but got a similar message about String.class.

Is there a way to use a custom equals based on the type in the expected object?

1

There are 1 best solutions below

0
Joel Costigliola On BEST ANSWER

It's not supported at the moment but you should be able to register a BiPredicate<String, Object> for String that would check if expected is a UUID and in this case call toString() on it.

Have a look at https://github.com/assertj/assertj/issues/3165 which is similar.