The Assertions class also has many overloaded methods. Each assertion method has at least two overloaded methods. Discover some of the more popular Assertions class methods and find out how to use them to perform unit tests.
The assertEquals Method
The JUnit 5 assertEquals method has over ten variations. This method is one of the more popular Assertions class methods. One variation of the assertEquals method takes an expected value and the function you want to evaluate (actual value). A second major variant takes an additional third argument. This is an error message that will display if the JUnit unit test fails.
The overloading of the assertEquals method happens with different data types. Some assertEquals overload methods take a fourth argument called delta. Other versions replace the failure string with a Supplier functional interface, in the form of a lambda expression.
AssertionsMethods Java Class
The AssertionsMethods Java class above has a single method called square. The square method takes an integer value and returns its square. To test the square method and any future method from the AssertionsMethods class you will need to create a JUnit test case.
The AssertionsMethodsTest JUnit Test Case
The testSquare() method uses three variations of assertEquals() to test the square() method. Each assertEquals() is a success, as all the expected values match the actual values returned by the square() method.
The assertNull Method
The JUnit Assertions class has exactly three assertNull methods. Each of these methods takes one or more arguments and asserts if the given object is null. If a given object is not null, then the test will fail.
The first assertNull() method takes a string object and checks if it is null. The second assertNull() method takes a string object and a string message to display if the test fails. The third and final assertNull() method takes the object you want to evaluate and a Supplier functional interface.
In the test case above the Supplier interface acts as an assignment target for a lambda expression. The lambda expression generates an error message if the test fails.
The assertTrue Method
The assertTrue() method has six variations. Each method asserts if a given condition is true. If an assertTrue() condition is false, the test will fail.
The testEvenNumbers() method demonstrates how to use all six assertTrue() methods. All the methods above are true, therefore, this unit test executes without failure or error.
assertTrue(Boolean condition): this method takes a Boolean condition and asserts if it is true.
The example of this method in the code above asserts if the first integer value is less than the second one.
assertTrue(Boolean condition, String message): this method takes a Boolean condition to test and a string to display if it’s false.
assertTrue(Boolean condition, Supplier
The assertFalse Method
The assertFalse() method is the opposite of the assertTrue() method. This method evaluates a given condition to see if it is false. If a given condition is true, then the assertFalse() test will fail. The assertFalse() method also has six variations that accept the same arguments as their assertTrue() counterparts.
The six assertFalse() methods in the testNotEvenNumbers() method all result in false, which means the assertFalse() tests are successful.
The Benefits of Unit Testing
Unit testing is an integral part of the software development process. Large software projects fail for various reasons, ranging from the teams that work on them to development approaches.
The purpose of unit testing is to eliminate software failure by providing early error detection. This requires teams to develop clear specifications, improve software design through error documentation, and provide support for software maintenance.
Unit testing is not the only software testing approach you should employ in your development life cycle, it’s just a very good place to start.