assertNotEquals() method
Explore the assertNotEquals() method in JUnit 5 assertions to validate that expected and actual values differ. Understand its usage through overloaded forms including custom messages and lazy evaluation with Supplier. Practice implementing this method in unit tests with practical examples using a StringUtils class, helping you write more robust and precise Java unit tests.
We'll cover the following...
assertNotEquals() method
Assertions API provide static assertNotEquals() method. This method helps us in validating that actual and expected values are not equal. This method uses equals() to assert the in-equality of actual and expected value.
- If the actual value is
not equalto expected value then the test case will pass. - If the actual value is
equalto expected value then the test case will fail.
There are basically three useful overloaded methods for assertNotEquals:-
-
assertNotEquals(Object expected, Object actual)- It assert whether expected and actual value arenotequal. -
assertNotEquals(Object expected, Object actual, String message)- It asserts whether expected and actual value arenotequal. In case, if the expected value isequalto actual value then the test case will fail with the provided message. -
assertNotEquals(Object expected, Object actual, Supplier<String> messageSupplier)...