assertj icon indicating copy to clipboard operation
assertj copied to clipboard

...usingRecursiveComparison().ignoringTransientFields()...

Open jchambers-crescendo opened this issue 1 year ago • 9 comments

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! :)

jchambers-crescendo avatar Feb 03 '24 22:02 jchambers-crescendo

@scordio I can add this if you think it is worth it.

pbacz avatar Feb 26 '24 14:02 pbacz

Thanks for the offer, @pbacz! Paging @joel-costigliola here

scordio avatar Feb 26 '24 14:02 scordio

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.

joel-costigliola avatar Mar 31 '24 22:03 joel-costigliola

@pbacz are you still interested contributing it ?

joel-costigliola avatar Mar 31 '24 22:03 joel-costigliola

@joel-costigliola Yes, I'd like to contribute.

pbacz avatar Apr 02 '24 14:04 pbacz

Cool, thanks @pbacz

joel-costigliola avatar Apr 02 '24 16:04 joel-costigliola

@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?

pbacz avatar Apr 02 '24 21:04 pbacz

@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

pbacz avatar Jul 03 '24 19:07 pbacz

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)

joel-costigliola avatar Jul 03 '24 21:07 joel-costigliola