Class ComparingNormalizedFields

java.lang.Object
org.assertj.core.api.recursive.comparison.ComparingNormalizedFields
All Implemented Interfaces:
RecursiveComparisonIntrospectionStrategy
Direct Known Subclasses:
ComparingSnakeOrCamelCaseFields

public abstract class ComparingNormalizedFields extends Object implements RecursiveComparisonIntrospectionStrategy
A RecursiveComparisonIntrospectionStrategy that introspects fields provided their normalized name.

Subclass this and simply provide the normalization to apply by implementing normalizeFieldName(String).

It is recommended to override getDescription() to get more informative error messages.

Since:
3.24.0
  • Constructor Details

    • ComparingNormalizedFields

      public ComparingNormalizedFields()
  • Method Details

    • getChildrenNodeNamesOf

      public Set<String> getChildrenNodeNamesOf(Object node)
      Returns the normalized names of the children nodes of the given object that will be used in the recursive comparison.

      The names are normalized according to normalizeFieldName(String).

      Specified by:
      getChildrenNodeNamesOf in interface RecursiveComparisonIntrospectionStrategy
      Parameters:
      node - the object to get the child nodes from
      Returns:
      the normalized names of the children nodes of the given object
    • normalizeFieldName

      protected abstract String normalizeFieldName(String fieldName)
      Returns the normalized version of the given field name to allow actual and expected fields to be matched.

      For example, let's assume actual is a Person with camel case fields like firstName and expected is a PersonDto with snake case field like first_name.

      The default recursive comparison gathers all actual and expected fields to compare them but fails as it can't know that actual.firstName must be compared to expected.first_name.
      By normalizing fields names first, the recursive comparison can now operate on fields that can be matched.

      In our example, we can either normalize fields to be camel case or snake case (camel case would be more natural though).

      Note that getChildNodeValue(String, Object) receives the normalized field name, it tries to get its value first and if failing to do so, it tries the original field name.
      In our example, if we normalize to camel case, getting firstName works fine for actual but not for expected, we have to get the original field name first_name to get the value (ComparingNormalizedFields implementation tracks which original field names resulted in a specific normalized field name).

      Parameters:
      fieldName - the field name to normalize
      Returns:
      the normalized field name
    • getChildNodeValue

      public Object getChildNodeValue(String fieldName, Object instance)
      Returns the value of the given object field identified by the fieldName parameter.

      Note that this method receives the normalized field name with (see normalizeFieldName(String)), it tries to get its value first and if failing to do so, it tries the original field name (ComparingNormalizedFields implementation tracks which original field names resulted in a specific normalized field name).

      For example, let's assume actual is a Person with camel case fields like firstName and expected is a PersonDto with snake case field like first_name and we normalize all fields names to be camel case. In this case, getting firstName works fine for actual but not for expected, for the latter it succeeds with the original field name first_name.

      Specified by:
      getChildNodeValue in interface RecursiveComparisonIntrospectionStrategy
      Parameters:
      fieldName - the field name
      instance - the object to read the field from
      Returns:
      the object field value
    • getDescription

      public String getDescription()
      Description copied from interface: RecursiveComparisonIntrospectionStrategy
      Returns a human-readable description of the strategy to be used in error messages.

      The default implementation returns this.getClass().getSimpleName().

      Specified by:
      getDescription in interface RecursiveComparisonIntrospectionStrategy
      Returns:
      a description of the strategy