Assertion in JUnit

In this blogpost, we are going to cover the use of Assert class and its useful methods.

Assert class provides a bunch of methods, used while writing test scripts in order to make a test case pass or fail, based on certain conditions.

In order to see all the assertion methods provided by JUnit in Assert class, just press control and space keys together after writing Assert.

Assertion-methods-in-junit.png

Assert-class-methods-in-junit.png

We can make use of these methods to make our test script pass or fail, based on the condition specified.

Assert.assertTrue(boolean condition):

The method asserts that the condition specified is TRUE, if the condition is not TRUE, it throws an AssertionError. An overloaded version of the method is also provided, which allows to specify the error message along when the condition is not TRUE.

Assert.assertTrue(String message, boolean condition);

Assert.assertFalse(boolean condition):

The method asserts that the condition specified is FALSE, if the condition is not FALSE, it throws an AssertionError. An overloaded version of the method is also provided, which allows to specify the error message along when the condition is not FALSE.

Assert.assertFalse(String message, boolean condition);

Assert.fail():

The method fails the test, without any condition specified. We can specify the failure message as well, using an overloaded version of the method.

Assert.fail(String message);

Assert.assertNull(Object obj):

The method asserts that the object specified is null, if it is not null, then it throws AssertionError.

Assert.assertNull(String message, Object obj):

The method works in the same way as the previous, but it throws AssertionError with the message specified if the object is not null.

Assert.assertNotNull(Object obj):

The method asserts that the object specified is not null, if it is null, then it throws AssertionError.

Assert.assertNotNull(String message, Object obj):

The method works in the same way as the previous, but it throws AssertionError with the message specified if the object is null.

Assert.assertEquals(expected, actual):

This is one of the important method of Assert class.

The method has many overloaded versions, which accepts different type of arguments like boolean, byte, char, short, int, long, double, float, double, String, Object etc.

Example: assertEquals method for boolean and double datatypes.

Assert.assertEquals(boolean expected, boolean actual);

Assert.assertEquals(double expected, double actual);

The method compares the expected value with the actual value, and throws AssertionException is both do not match.

Assert.assertEquals(String message, expected, actual):

The method works in the same way as the previous, but it throws AssertionError with the message specified if the expected and actual values are not the same.

Here is the sample code snippet containing several assert methods.

import org.junit.Test;
import junit.framework.Assert;

public class AssertionDemo {

@Test
public void testMethod() {
boolean b1 = (5 > 4);
boolean b2 = “hello”.equalsIgnoreCase(“world”);
String s1 = “test”;
String s2 = “test”;
String s3 = “testing”;
Object o1 = null;

Assert.assertTrue(b1);
Assert.assertFalse(b2);
Assert.assertEquals(s1, s2);
Assert.assertEquals(“String values are not same”, s1, s3);

Assert.assertNull(o1);
Assert.assertNotNull(o1);

}
}

Hope it works for you!!

Cheers 🙂

Advertisements

#assert, #assert-in-junit, #assertequals, #assertfalse, #assertion-methods, #assertnotnull, #assertnull, #asserttrue, #fail