Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical uses for the option to pass `Supplier` as a message supplier in JUnit 5

The Assertions class in JUnit 5 allows for passing an Supplier<String> as a messageSupplier, an object that provides the text of a message to report when the test fails.

For example, assertEquals:

public static void assertEquals​( char expected,
                                 char actual,
                                 Supplier<String> messageSupplier )

I am wondering what the practical use of such a supplier might be, specifically in the context of unit testing.

I can imagine perhaps localizing the strings, though that seems a bit strange to localize when the audience is the members of a development project.

➥ Are there any other practical uses of passing such a message supplier rather than hard-coding message string?

like image 501
Basil Bourque Avatar asked Jan 01 '23 06:01

Basil Bourque


1 Answers

When building message is expensive

If I remember correctly, we - the JUnit 5 team - introduced the supplier variant for cases in which building the message string is costly, eg due to accessing a database. You’d only want to do this if necessary, ie in case of failure.

like image 133
johanneslink Avatar answered Jan 14 '23 02:01

johanneslink