The JUnit Assertions class contains a collection of static methods that allow you to perform unit tests. Assertions are one of the main features of JUnit. This class has over fifty different methods. Some tests of the Assertions class will fail if a condition is true, while others will fail if a condition is false.
The Assertions class also has many overloaded methods. Every assertion method has at least two overloaded methods. Discover some of the most popular assertion class methods and learn how to use them for unit testing.
The assertion method equals
JUnit 5’s assertEquals method has more than ten variations. This method is one of the most popular assertion class methods. A variation of the assertEquals method takes an expected value and the function you want to evaluate (actual value). A second important variant takes an additional third argument. This is an error message that will be displayed if the JUnit unit test fails.
The assertEquals method overload occurs with different data types. Some overload methods of assertEquals take a fourth argument called delta. Other versions replace the error string with a functional provider interface, in the form of a lambda expression.
Assertions Methods Java Class
package com.program;public class AssertionsMethods {
public static int square(int num) {
return num * num;
}
}
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 methods of the AssertionsMethods class, you will need to create a JUnit test case.
AssertionsMethodsTest JUnit test case
package com.program;import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class AssertionsMethodsTest {
@Test
void testSquare() {
assertEquals(25, AssertionMethods.square(5));
assertEquals(36, AssertionMethods.square(6), "Your square values did not match.");
assertEquals(49, AssertionMethods.square(7), () -> "Your square values did not match.");
}
}
The testSquare() method uses three variations of assertEquals() to test the square() method. Each assertEquals() is a success, since all expected values match the actual values returned by the square() method.
The assertNull method
The JUnit assert class has exactly three assertNull methods. Each of these methods takes one or more arguments and asserts whether the given object is null. If a given object is not null, the test will fail.
@Test
public void testStringValue() {
String stringValue = null;
assertNull(stringValue);
assertNull(stringValue, "Your string value is not null");
assertNull(stringValue, () -> "Your string value is not null");
}
The first method assertNull() 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 functional provider interface.
In the test case above, the provider interface acts as an assignment target for a lambda expression. The lambda expression generates an error message if the test fails.
The true statement method
The assertTrue() method has six variations. Each method asserts whether a given condition is true. If a condition of assertTrue() is false, the test will fail.
@Test
void testEvenNumbers() {
int num1 = 10;
int num2 = 16;
int num3 = 26;
assertTrue(num1 < num2);
assertTrue(num3 > num2, "Your condition is not true.");
assertTrue(num1 < num3, () -> " Your condition is not true.");
assertTrue(() -> num1%2 == 0);
assertTrue(() -> num2%2 == 0, "Your value is not an even number.");
assertTrue(() -> num3%2 == 0, () -> "Your value is not an even number.");
}
The testEvenNumbers() method demonstrates how to use all six assertTrue() methods. All of the above methods are true, therefore this unit test runs without any failures or errors.
- assertTrue (boolean condition): This method takes a boolean condition and asserts whether it is true. The example of this method in the code above asserts if the first integer value is less than the second.
- assertTrue(boolean condition, string message): This method takes a boolean condition to test and a string to return if false.
- assertTrue(Boolean condition, provider
messageSupplier): This method takes a boolean functional interface and provider, such as a lambda expression. The message provider contains a string to display if the boolean condition is false. - assertTrue(BooleanSupplier booleanSupplier): This method takes a BooleanSupplier functional interface, in the form of a lambda expression that evaluates to true or false. The code example of this method uses a lambda expression. Check the remainder of the first integer divided by two to determine if it is odd or even.
- assertTrue(BooleanSupplier booleanSupplier, String message): This method takes a BooleanSupplier functional interface in the form of a lambda expression. A string message is also required to print if BooleanSupier is not true.
- assertTrue(BooleanSupplier booleanSupplier, Supplier
messageSupplier): This method takes a BooleanSupplier functional interface to assert. It also requires a functional provider interface, which is a lambda expression that will print a string value if the test fails.
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 assertionFalse() test will fail. The assertFalse() method also has six variations that accept the same arguments as their assertTrue() counterparts.
@Test
void testNotEvenNumbers() {
int num1 = 11;
int num2 = 17;
int num3 = 27;
assertFalse(num2 < num1);
assertFalse(num2 > num3, " Your condition is not false.");
assertFalse(num3 < num1, () -> " Your condition is not false.");
assertFalse(() -> num1%2 == 0);
assertFalse(() -> num2%2 == 0, "Your value is an even number.");
assertFalse(() -> num3%2 == 0, () -> "Your value is an even number.");
}
All six assertFalse() methods in the testNotEvenNumbers() method return false, meaning that assertFalse() tests succeed.
The benefits of unit tests
Unit tests are an integral part of the software development process. Big software projects fail for a variety of reasons, ranging from the teams working on them to development approaches.
The purpose of unit testing is to eliminate software flaws by providing early error detection. This requires teams to develop clear specifications, improve software design through bug documentation, and provide software maintenance support.
Unit testing isn’t the only software testing approach you should employ in your development lifecycle, it’s just a great place to start.