chrxsDeveloper
chrxsDeveloper

Reputation: 29

Translate isEqualToComparingFieldByFieldRecursively to my Spock Test

I am translating all my Junit Tests into Spock Tests and I don't want to use any "Assert methods". Therefore I have to translate the method isEqualToComparingFieldByFieldRecursively into my Groovy class. I am not sure, if I understood the function of the method correctly.

assertThat(validationMessage).isEqualToComparingFieldByFieldRecursively(provider.createValidationMessageDto());

In this case validationMessage is a DTO-Object and provider.createValidationMessageDto returns the same kind of DTO-Object. Does there already exist any similar method or do I have to code a new own method, which checks validationMessage? And if so, what should the method do?

Thanks for your answers. 😊

Upvotes: 0

Views: 499

Answers (1)

Leonard Brünings
Leonard Brünings

Reputation: 13242

If the validation message DTO doesn't provide a proper equals methods, then sticking with isEqualToComparingFieldByFieldRecursively might be your best solution. While Spock has great implicit assertion, something like reflective field-by-field compare isn't directly supported.

You could use Spock's support for Hamcrest and it's samePropertyValuesAs to write:

import static org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs
import static spock.util.matcher.HamcrestSupport.that
// ...
expect:
that validationMessage, samePropertyValuesAs(provider.createValidationMessageDto()

However, that does not do recursive comparison, for that you could use https://github.com/shazam/shazamcrest

Upvotes: 1

Related Questions