Friday 29 June 2018

Junit: Assert null values

Junit provides below methods to assert null values.


static public void assertNotNull(String message, Object object)
Asserts that an object isn't null. If the object is null, then junit throws assertion error with given message.

static public void assertNotNull(Object object)
Asserts that an object isn't null. If the object is null, then junit throws assertion error without message.

static public void assertNull(String message, Object object)
Asserts that an object is null. If the object is not null, then junit throws assertion error with given message.

static public void assertNull(Object object)
Asserts that an object is null. If the object is not null, then junit throws assertion error without message.

Find the below working application.

DemoTestApp.java

package com.sample.arithmetic;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.junit.Test;

public class DemoTestApp {

 @Test
 public void testAssertNotNullWithMessage() {
  assertNotNull("Object is null", new Object());
 }

 @Test
 public void testAssertNotNullWithOutMessage() {
  assertNotNull(new Object());
 }

 @Test
 public void testAssertNullWithMessage() {
  assertNull("Object is not null", null);
 }

 @Test
 public void testAssertNullWithOutMessage() {
  assertNull(null);
 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment