|
Answer» Assert methods are utility methods that support assert conditions in test cases. They belong to the Assert class in JUnit 4 and the Assertions class in JUnit 5. It is recommended to import the assert methods statically to the test class for avoiding using the class as a prefix to the method. Let us consider an example of the Multiplier class: public class Multiplier{ public int multiply(int num1, int num2){ return num1 * num2; }}Following are some of the assert methods: - assertEquals(): This method compares 2 objects for equality by making use of the equals() method of the object. This is shown in the test case that multiplies 2 methods and checks for the expected and actual value below.
@Test @DisplayName("Multiplying 2 numbers") public void multiplyTest() { assertEquals(50, multiplier.multiply(10, 5)); } When two objects are found to be equal based on the equals() method implementation of the object, then assertEquals() returns normally. Else, an exception will be thrown and the test will stop its execution. - assertTrue(): This method tests whether the value of a variable is true.
@Test public void checkNumTest() { int num1 = 20; assertTrue("Number is not equal to 0", num1!=0);} If the ASSERTION fails, then an exception will be thrown and the test execution will be stopped. - assertFalse(): This method tests whether the value of a variable is FALSE.
@Test public void checkNumTest() { int num1 = -20; assertFalse("Number is not greater than 0",num1>0);} If the assertion fails, then an exception will be thrown and the test execution will be stopped. - assertNull(): This method tests if a variable is null. If null, it returns normally else an exception will be thrown and the test stops execution.
@Test public void checkNullTest() { int num1 = null; assertNull("Number is null",num1);}- assertNotNull(): This method tests whether a variable is not null. If it is not null, then the test returns normally else an exception will be thrown and the test stops its execution.
@Test public void checkNotNullTest() { int num1 = null; assertNotNull("Number is null",num1);}- assertSame(): This method checks if two references of the object are pointing to the same object.
@Test public void checkAssertSameTest() { Object num1 = new Object(); Object num2 = new Object(); assertSame(num1, num2); } If the object references are pointing to the same object, the test runs normally else, it will throw an exception and the test execution is aborted. - assertNotSame(): This method checks if two references of an object are not pointing to the same object.
@Test public void checkAssertSameTest() { Object num1 = new Object(); Object num2 = new Object(); assertSame(num1, num2);} If the object references are not pointing to the same object, the test runs normally else, it will throw an exception and the test execution is aborted. - assertThat(): This method compares the object to org.hamcrest.Matcher for checking if it matches WHATEVER the Matcher class requires for the object to match. Consider the following CODE.
import static org.hamcrest.MatcherAssert.assertThat;import static org.hamcrest.Matchers.hasItems;import org.junit.jupiter.api.Test;public class MultiplierTest { @Test public void assertThatTest() { assertThat( Arrays.asList(1,2,3,4), hasItems(2,3)); }}In this code, we will be asserting that the list has some items specified in the hamcrest’s hasItems() method. If the assertThat is successful, i.e if the list indeed has items specified, then the test runs normally. Else, the test throws an exception and the test stops executing.
|