assertEquals() method
Explore how to use the assertEquals() method in JUnit 5 to validate that actual and expected values match in unit tests. This lesson covers all overloaded forms of assertEquals, including those with error messages and supplier-based lazy evaluation, helping you write clear and effective equality assertions in your tests.
We'll cover the following...
assertEquals() method
Assertions API provide static assertEquals() method. This method helps us in validating that actual and expected values are equal. This method uses equals() to assert the equality of actual and expected value.
- If the actual value is
equalto expected value then the test case will pass. - If the actual value is
not equalto expected value then the test case will fail.
There are basically three useful overloaded methods for assertEquals:-
-
assertEquals(Object expected, Object actual)- It asserts whether expected and actual value are equal. -
assertEquals(Object expected, Object actual, String message)- It asserts whether expected and actual value are equal. In case, if the expected value is not equal to actual value then test case will fail with a provided message. -
assertEquals(Object expected, Object actual, Supplier<String> messageSupplier)- It assert whether expected and actual value are ...