Top Description Fields Methods
org.assertj.core.presentation

public Interface Representation

Known Direct Implementers
org.assertj.core.presentation.StandardRepresentation, org.assertj.core.presentation.CompositeRepresentation
Imports
java.util.ServiceLoader, org.assertj.core.api.Assertions, org.assertj.core.configuration.Configuration

Controls the formatting (String representation) of types in assertion error messages.

There are several ways to replace the StandardRepresentation as the default Representation:

The advantage of registering a representation (or a configuration overriding the default representation) is that you don't need to do anything in your tests, the java runtime will discover it and AssertJ will use it but it requires a bit more work than a simple call to Assertions#useRepresentation(Representation).

Note that a Configuration overriding the default representation takes precedence over any registered representation.

To register a Representation, you need to do several things:

The assertj-examples project provides a working example of registering a custom representation.

Registering a representation has been introduced in AssertJ 2.9.0/3.9.0.

Since 3.22.0, AssertJ can load multiples representations from the classpath, the idea behind is that different domain-specific libraries would be able to independently register representations for their respective domain. AssertJ aggregate them in a CompositeRepresentation which loops over the different representations and use the first non null representation value of the variable to display. If multiples representations overlap the highest priority one wins (see getPriority()). The StandardRepresentation is the fallback option when all the registered representations returned a null representation of the value to display (meaning they did not know how to represent the value).

Author
Mariusz Smykula

Field Summary

Modifier and TypeField and Description
public static final int

Method Summary

Modifier and TypeMethod and Description
public default int

Returns:

the representation priority.
getPriority
()

In case multiple representations are loaded through ServiceLoader and they can represent the same types the one with the highest priority is selected.

public String

Returns:

the toString representation of the given object.
toStringOf
(Object
the object to represent.
object
)

Returns the String representation of the given object.

public default String

Returns:

the unambiguous toString representation of the given object.
unambiguousToStringOf
(Object
the object to represent.
object
)

Override this method to return a String representation of the given object that is unambigous so that it can be differentiated from other objects with the same toStringOf(Object) representation.

Field Detail

DEFAULT_PRIORITYback to summary
public static final int DEFAULT_PRIORITY

Method Detail

getPriorityback to summary
public default int getPriority()

In case multiple representations are loaded through ServiceLoader and they can represent the same types the one with the highest priority is selected. If representations have the same priority, there is no guarantee which one is selected (but one will).

The StandardRepresentation is the fallback option when all the registered representations returned a null representation of the value to display (meaning they did not know how to represent the value).

The default priority is 1.

Returns:int

the representation priority.

toStringOfback to summary
public String toStringOf(Object object)

Returns the String representation of the given object. It may or may not be the object's own implementation of toString.

Parameters
object:Object

the object to represent.

Returns:String

the toString representation of the given object.

unambiguousToStringOfback to summary
public default String unambiguousToStringOf(Object object)

Override this method to return a String representation of the given object that is unambigous so that it can be differentiated from other objects with the same toStringOf(Object) representation.

The default implementation calls toStringOf(Object) but the StandardRepresentation adds the object hexadecimal identity hash code.

Parameters
object:Object

the object to represent.

Returns:String

the unambiguous toString representation of the given object.