There are several ways to replace the StandardRepresentation as the default Representation:
Assertions#useRepresentation(Representation), from this point all assertions will use the given representation.Configuration overriding the default representation specified with Configuration#representation() as explained here.
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:
org.assertj.core.presentation.Representation file in META-INF/services directoryRepresentation in itMETA-INF/services/org.assertj.core.presentation.Representation is in the runtime classpath, usually putting it in src/test/resources is enoughThe 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).
| Modifier and Type | Field and Description |
|---|---|
| public static final int |
| Modifier and Type | Method and Description |
|---|---|
| public default int | Returns: the representation priority.In case multiple representations are loaded through |
| public String | Returns: thetoString representation of the given object.the object to represent. object)Returns the |
| public default String | Returns: the unambiguoustoString representation of the given object.the object to represent. object)Override this method to return a |
| DEFAULT_PRIORITY | back to summary |
|---|---|
| public static final int DEFAULT_PRIORITY | |
| getPriority | back to summary |
|---|---|
| public default int getPriority() In case multiple representations are loaded through
The The default priority is 1.
| |
| toStringOf | back to summary |
|---|---|
| public String toStringOf(Object object) Returns the | |
| unambiguousToStringOf | back to summary |
|---|---|
| public default String unambiguousToStringOf(Object object) Override this method to return a
The default implementation calls | |