assertj
assertj copied to clipboard
...usingRecursiveComparison().ignoringTransientFields()...
Feature summary
Pretty much the title, I would find it very convenient to be able to simple exclude fields based on their modifier, such as transient
in my current use case, but why not any given set of modifiers?
Example
Could be as in title, or something like:
assertThat(actual).usingRecursiveComparison().ignoringFieldsWithModifiers(int modifiers);
This could be the same structure of int
that java.lang.reflect.Modifier
takes.
Just an idea, and seems straightforward. Thanks for listening! :)
@scordio I can add this if you think it is worth it.
Thanks for the offer, @pbacz! Paging @joel-costigliola here
Since there are not so many modifiers that seems relevant to ignore besides transient
(volatile maybe, but I can't really see a use case for it) then I would prefer to simply add ignoringTransientFields()
which is a bit easier to discover that ignoringFieldsWithModifiers
We can add ignoringFieldsWithModifiers
later if we find a real use case for it.
@pbacz are you still interested contributing it ?
@joel-costigliola Yes, I'd like to contribute.
Cool, thanks @pbacz
@joel-costigliola @scordio What would be the expected behavior when:
- a field is transient in actual but not in expected
- a field is transient in expected but not in actual?
@joel-costigliola @scordio
I have an additional question.
What should happen when ignoringTransientFields()
is used while the COMPARING_PROPERTIES
introspection strategy is used? It doesn't make sense to explicitly ignore transient fields when the assertion doesn't look at the fields in the first place.
assertThat(actual)
.usingRecursiveComparison()
.withIntrospectionStrategy(COMPARING_PROPERTIES)
.ignoringTransientFields()
.isEqualTo(expected);
What should happen here?
- Ignore it, the user knows what they are doing
- Log a warning
- Make the assertion fail
At the moment the recursive comparison ignore fields from actual (unless specific case where you can ignore null expected fields), to be consistent we should do the same thing.
For the second question, I think we should either ignore or log a warning (we don't log much in assertj so another option would be in the recursive comparison description that comes with the error message)