AssertJ custom representation of asserted object
I really enjoy asserting collections with AssertJ. Usually it’s safer and simpler to use containsExactlyInAnyOrder
rather than asserting individual elements (collection.get(0)
) - even if there is only one element, because it may change in the future. The challenge starts when asserting collection containing complex objects, because constructing an entire object as expected element can be tedious. To extract only certain properties, we can use .extracting(Foo::field1, Foo::field2)
.
1
2
3
4
5
6
7
8
9
var players = List.of(
new Player("Michael Jordan", new Team("Bulls")),
new Player("Kobe Bryant", new Team("Lakers")));
assertThat(players)
.extracting(Player::name, player -> player.team().name())
.containsExactly(
tuple("Michael Jordan", "Bulls"),
tuple("Kobe Bryant", "Lakers"));
However, I tend to concatenate properties to string because I wasn’t fond of working with tuples:
1
2
3
4
assertThat(players)
.extracting(player -> player.name() + " | " + player.team().name())
.containsExactly("Michael Jordan | Bulls",
"Kobe Bryant | Lakers");
The reason is that, by default, AssertJ provides a generic “tuple” representation in the “actual” section. When copied, this has to be manually adapted to Java code, which can be inconvenient.
For example:
1
2
3
Expecting actual:
[("Michael Jordan", "Bulls"),
("Kobe Bryant" "Lakers")]
What I want is an “easy-to-copy” representation of the asserted object:
1
2
3
Expecting actual:
[tuple("Michael Jordan", "Bulls"),
tuple("Kobe Bryant" "Lakers")]
Fortunately, there’s an easy way to globally fix this in 3 simple steps.
- Define a custom representation:
1 2 3 4 5 6 7 8 9
class CustomAssertJRepresentation extends StandardRepresentation { static final CustomAssertJRepresentation INSTANCE = new CustomAssertJRepresentation(); @Override protected String toStringOf(Tuple tuple) { return "tuple" + super.toStringOf(tuple); } }
- Then add it to the global configuration:
1 2 3 4 5 6 7 8 9 10 11 12
public class CustomAssertJConfiguration extends Configuration { @Override public Representation representation() { return CustomAssertJRepresentation.INSTANCE; } @Override public String describe() { return "CustomAssertJConfiguration applied"; } }
- Lastly, register the global config in this file:
/src/test/resources/META-INF/services/org.assertj.core.configuration.Configuration
which will contain:1
my.package.CustomAssertJConfiguration
Refer to the official documentation for more information: https://assertj.github.io/doc/#assertj-core-representation