From the course: Complete Guide To Java Testing with JUnit 5 & Mockito

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Message suppliers

Message suppliers

- [Instructor] When an assertion fails, you often want to provide a custom error message to make it clear what went wrong. Traditionally, this is done by passing a string message directly to the assertion message. However, constructing error messages can be expensive, especially if they involve string concatenation or other operations. There's also no guarantee you even need it if the test passes. This is where message suppliers come in. A message supplier is a lambda or method reference that provides the error message only if the assertion fails. This means the message construction is deferred until it's actually needed, making your tests more efficient. Let's take a look at some examples. Here we have a basic calculator class. It also has a test class with an assertion and an assertion message. Here, the assertion message is constructed immediately, regardless of whether the assertion passes or fails, this construction involves allocating memory for the new string and performing the…

Contents