Interface Representation
- All Known Implementing Classes:
BinaryRepresentation,CompositeRepresentation,HexadecimalRepresentation,StandardRepresentation,UnicodeRepresentation
There are several ways to replace the StandardRepresentation as the default Representation:
- call
Assertions.useRepresentation(Representation), from this point all assertions will use the given representation. - use a
Configurationoverriding the default representation specified withConfiguration.representation()as explained here. - register a representation as a service discovered at program startup.
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:
- create a file named
org.assertj.core.presentation.Representationfile in META-INF/services directory - put the fully qualified class name of your
Representationin it - make sure
META-INF/services/org.assertj.core.presentation.Representationis in the runtime classpath, usually putting it insrc/test/resourcesis enough
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
Fields -
Method Summary
Modifier and TypeMethodDescriptiondefault intIn case multiple representations are loaded throughServiceLoaderand they can represent the same types the one with the highest priority is selected.toStringOf(Object object) Returns theStringrepresentation of the given object.default StringunambiguousToStringOf(Object object) Override this method to return aStringrepresentation of the given object that is unambigous so that it can be differentiated from other objects with the sametoStringOf(Object)representation.
-
Field Details
-
DEFAULT_PRIORITY
static final int DEFAULT_PRIORITY- See Also:
-
-
Method Details
-
toStringOf
Returns theStringrepresentation of the given object. It may or may not be the object's own implementation oftoString.- Parameters:
object- the object to represent.- Returns:
- the
toStringrepresentation of the given object.
-
unambiguousToStringOf
Override this method to return aStringrepresentation of the given object that is unambigous so that it can be differentiated from other objects with the sametoStringOf(Object)representation.The default implementation calls
toStringOf(Object)but theStandardRepresentationadds the object hexadecimal identity hash code.- Parameters:
object- the object to represent.- Returns:
- the unambiguous
toStringrepresentation of the given object.
-
getPriority
default int getPriority()In case multiple representations are loaded throughServiceLoaderand 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
StandardRepresentationis 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:
- the representation priority.
-