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
From the course: Complete Guide To Java Testing with JUnit 5 & Mockito
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
-
-
-
-
-
-
-
(Locked)
Add a display name to your tests1m 30s
-
(Locked)
Nested tests3m 31s
-
(Locked)
Timeout1m 47s
-
(Locked)
AssertTimeout2m 31s
-
(Locked)
AssertAll3m 50s
-
(Locked)
Message suppliers2m 3s
-
(Locked)
Assumptions API2m 57s
-
(Locked)
TempDir7m 15s
-
(Locked)
Migrate from JUnit 4 to JUnit 52m 52s
-
(Locked)
Challenge: Sorting algorithm validation55s
-
(Locked)
Solution: Sorting algorithm validation6m 47s
-
(Locked)
-
-
-
-