Assertion in JUnit Framework

Assertion in JUnit Framework. In this article, we will discuss some of the Assertion methods provided by the JUnit framework.

Assertion in JUnit Framework

Assertion is a class provided by the JUnit framework which allows us to test a code. There are multiple methods in the Assert class. org.junit.Assert.* is the package where we have all these methods of an Assertion class. Following is the declaration of the class:

public class Assert extends java.lang.Object

Following are the methods provided by this class:

  • assertEquals(double expected, double actual): This method checks if both the expected and actual output is the same or not. It has lots of overridden methods. The expected value is the value that we want a method to return and the actual value is the value that the method is returning.

  • assertTrue(boolean condition): This method checks if the condition is true or not. If we have the method that to be tested is returning a boolean value then we can use the assertTrue() method. We have two overloaded methods for this
assertTrue(boolean condition);
assertTrue(String message, boolean condition)
  • assertFalse(boolean condition): This method checks if the condition is false or not. If we have the method that to be tested is returning a boolean value then we can use the assertFalse() method. We have two overloaded methods for this
assertFalse(boolean condition);
assertFalse(String message, boolean condition)
  • assertNotNull(Object object): This method checks if the object that we passed as an argument is not null. We have two overloaded methods for this
assertNotNull(Object object);
assertNotNull(String message, Object object);
  • assertNull(Object object): This method checks if the object that we passed as an argument is null. We have two overloaded methods for this
assertNull(Object object); 
assertNull(String message, Object object);
  • fail(): This method fails a test.

If you want to see the Example of the assertEquals() method check this article How to Write your first JUnit Test Case.

Example of assertTrue() and assertFalse() Method

In this example, we will create a method that will return true if the number is even.

Step 1: Open Eclipse > File > New > Maven Project.

Step 2: Now, you will get a New Maven Project. Check the create a simple project. Click on Next.

Step 3: Now, Enter group id, artifact id as shown below

Step 4: Now, create a NumberUtil.java class inside the src/main/java folder.

package in.codedec.junit.util;

public class NumberUtil {

    public boolean isEvenNumber(int number){
        return number%2==0;
    }
}
  • Here, in this class, we created a method called isEvenNumber() that will check if the number is even or not.
  • The method is returning a boolean value.

Step 5: Now, it is time to check this method. So we will create our JUnit Test case. Here, we will keep our test file in a separate package. Mostly, we keep the test case file inside src/test/java. After that, click Finish. After Finish you will get the alert box, just click OK.

Step 6: Now, we will get the default test case generated and we will modify the code as shown below

package in.codedec.junit.util;

import static org.junit.Assert.*;

import org.junit.Test;

public class NumberUtilTest {

    @Test
    public void test() {
        NumberUtil numberUtil = new NumberUtil();
        assertTrue(numberUtil.isEvenNumber(2));
    }

}
  • On-line no:11, we have created the object of the class that needs to be tested.
  • On-line no:12, the isEvenNumber() method is called in assertTrue() method to check if the condition is true.

Step 7: Now, Run the Application as Junit Test and see the following stack trace.

Here, we can see the green bar that implies the test is run without error.

Now, let us look at the assertFalse() method. The isEvenNumber() method is passed with an odd number. Now, this method will return false and assertTrue() methods to check if the condition is false or not. Thus, our test case is successfully run without failures.

package in.codedec.junit.util;

import static org.junit.Assert.*;

import org.junit.Test;

public class NumberUtilTest {

    @Test
    public void test() {
        NumberUtil numberUtil = new NumberUtil();
        assertFalse(numberUtil.isEvenNumber(5));
    }

}

Now, Run the Application as JUnit Test and see the following stack trace.

Thus, this is How we use the assertion method of the Assertion Class in JUnit Framework. In the next article of this tutorial, we will see some of the annotations in the JUnit framework.