Package org.assertj.core.presentation
Interface Representation
- All Known Implementing Classes:
BinaryRepresentation
,HexadecimalRepresentation
,StandardRepresentation
,UnicodeRepresentation
public interface Representation
Controls the formatting (String representation) of types in assertion error message.
There are two ways to replace the default Representation
(StandardRepresentation
):
- call
Assertions.useRepresentation(Representation)
, from this point all the assertions will use the given representation - register a representation as a service discovered at program startup
The advantage of registering a 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)
.
To register a Representation
, you need to do several things:
- create a file named
org.assertj.core.presentation.Representation
file in META-INF/services directory - put the fully qualified class name of your
Representation
in it - make sure
META-INF/services/org.assertj.core.presentation.Representation
is in the runtime classpath, usually putting it insrc/test/resources
is enough - we recommend that you extend from the
StandardRepresentation
and override theStandardRepresentation.fallbackToStringOf(Object)
. By doing this all the defaults of AssertJ would be applied and you can apply your own customization
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.
-
Method Summary
Modifier and TypeMethodDescriptiontoStringOf
(Object object) Returns theString
representation of the given object.unambiguousToStringOf
(Object object) Returns theString
representation of the given object with its type and hexadecimal identity hash code so that it can be differentiated from other objects with the sametoStringOf(Object)
representation.
-
Method Details
-
toStringOf
Returns theString
representation of the given object. It may or may not be the object's own implementation oftoString
.- Parameters:
object
- the object to represent.- Returns:
- the
toString
representation of the given object.
-
unambiguousToStringOf
Returns theString
representation of the given object with its type and hexadecimal identity hash code so that it can be differentiated from other objects with the sametoStringOf(Object)
representation.- Parameters:
object
- the object to represent.- Returns:
- the unambiguous
toString
representation of the given object.
-