Top Description Fields Constructors Methods
org.assertj.core.api.recursive.comparison

public abstract Class ComparingNormalizedFields

extends Object
implements RecursiveComparisonIntrospectionStrategy
Class Inheritance
All Implemented Interfaces
org.assertj.core.api.recursive.comparison.RecursiveComparisonIntrospectionStrategy
Known Direct Subclasses
org.assertj.core.api.recursive.comparison.ComparingSnakeOrCamelCaseFields
Static Imports
java.lang.String.format, java.util.stream.Collectors.toSet, org.assertj.core.util.introspection.PropertyOrFieldSupport.COMPARISON

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

Field Summary

Modifier and TypeField and Description
private final Map<Class<?>, Set<String>>
private static final String
private final Map<Object, Map<String, String>>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

the object field value
getChildNodeValue
(String
the field name
fieldName
,
Object
the object to read the field from
instance
)

Implements org.assertj.core.api.recursive.comparison.RecursiveComparisonIntrospectionStrategy.getChildNodeValue.

Returns the value of the given object field identified by the fieldName parameter.
public Set<String>

Returns:

the normalized names of the children nodes of the given object
getChildrenNodeNamesOf
(Object
the object to get the child nodes from
node
)

Implements org.assertj.core.api.recursive.comparison.RecursiveComparisonIntrospectionStrategy.getChildrenNodeNamesOf.

Returns the normalized names of the children nodes of the given object that will be used in the recursive comparison.
public String
getDescription()

Overrides default org.assertj.core.api.recursive.comparison.RecursiveComparisonIntrospectionStrategy.getDescription.

Returns a human-readable description of the strategy to be used in error messages.
private String
getOriginalFieldName(String fieldName, Object instance)

private String

Returns:

the normalized field name
normalize
(Object node, String
the field name to normalize
fieldName
)

Normalize the field name and keep track of the normalized name -> original name

protected abstract String

Returns:

the normalized field name
normalizeFieldName
(String
the field name to normalize
fieldName
)

Returns the normalized version of the given field name to allow actual and expected fields to be matched.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fieldNamesPerClassback to summary
private final Map<Class<?>, Set<String>> fieldNamesPerClass
NO_FIELD_FOUNDback to summary
private static final String NO_FIELD_FOUND
originalFieldNamesByNormalizedFieldNameByNodeback to summary
private final Map<Object, Map<String, String>> originalFieldNamesByNormalizedFieldNameByNode

Constructor Detail

ComparingNormalizedFieldsback to summary
public ComparingNormalizedFields()

Method Detail

getChildNodeValueback to summary
public Object getChildNodeValue(String fieldName, Object instance)

Implements org.assertj.core.api.recursive.comparison.RecursiveComparisonIntrospectionStrategy.getChildNodeValue.

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.

Parameters
fieldName:String

the field name

instance:Object

the object to read the field from

Returns:Object

the object field value

Annotations
@Override
getChildrenNodeNamesOfback to summary
public Set<String> getChildrenNodeNamesOf(Object node)

Implements org.assertj.core.api.recursive.comparison.RecursiveComparisonIntrospectionStrategy.getChildrenNodeNamesOf.

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).

Parameters
node:Object

the object to get the child nodes from

Returns:Set<String>

the normalized names of the children nodes of the given object

Annotations
@Override
getDescriptionback to summary
public String getDescription()

Overrides default org.assertj.core.api.recursive.comparison.RecursiveComparisonIntrospectionStrategy.getDescription.

Doc from org.assertj.core.api.recursive.comparison.RecursiveComparisonIntrospectionStrategy.getDescription.

Returns a human-readable description of the strategy to be used in error messages.

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

Returns:String

a description of the strategy

Annotations
@Override
getOriginalFieldNameback to summary
private String getOriginalFieldName(String fieldName, Object instance)
normalizeback to summary
private String normalize(Object node, String fieldName)

Normalize the field name and keep track of the normalized name -> original name

Parameters
fieldName:String

the field name to normalize

Returns:String

the normalized field name

normalizeFieldNameback to summary
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:String

the field name to normalize

Returns:String

the normalized field name