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

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 in src/test/resources is enough
  • we recommend that you extend from the StandardRepresentation and override the StandardRepresentation.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 Type
    Method
    Description
    Returns the String representation of the given object.
    Returns the String representation of the given object with its type and hexadecimal identity hash code so that it can be differentiated from other objects with the same toStringOf(Object) representation.
  • Method Details

    • toStringOf

      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 - the object to represent.
      Returns:
      the toString representation of the given object.
    • unambiguousToStringOf

      String unambiguousToStringOf(Object object)
      Returns the String representation of the given object with its type and hexadecimal identity hash code so that it can be differentiated from other objects with the same toStringOf(Object) representation.
      Parameters:
      object - the object to represent.
      Returns:
      the unambiguous toString representation of the given object.