JUnit Assert.assertNotNull() Method Example

assertNotNull() method belongs to JUnit 4 org.junit.Assert class. In JUnit 5 all JUnit 4 assertion methods are moved to org.junit.jupiter.api.Assertions class.

void org.junit.Assert.assertNotNull(Object object)

Asserts that an object isn't null. If it is an AssertionError is thrown. Parameters:
  • object - Object to check or null

Assert.assertNotNull(Object object) Method Example

import static org.junit.Assert.assertNotNull;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;

import com.javaguides.strings.StringUtility;

public class AssertNotNullExample {
    public static String[] toStringArray(final Collection<?> collection) {
        if (collection == null) {
             return null;
        }
       return collection.toArray(new String[collection.size()]);
    }

 @Test
 public void toStringArrayTest() {
      final String[] strArray = StringUtility.toStringArray(Arrays.asList("a", "b", "c"));
      for (final String element : strArray) {
           assertNotNull(element);
      }
   }
}

Output:



Related JUnit Examples


Comments