JUnit 5 assertNotNull Example

In this article, we will learn about assertNotNull() static method which belongs to JUnit 5 org.junit.jupiter.api.Assertions Class. Note that in JUnit 5 all JUnit 4 assertion methods are moved to org.junit.jupiter.api.Assertions class.
There are many overloaded version of assertNotNull() methods present in org.junit.jupiter.api.Assertions class.
Let me list out tools and technologies that I have used to develop JUnit 5 assertNotNull Example.

Tools and Technologies Used

  1. Eclipse photon (only this eclipse version supports JUnit 5)
  2. Maven
  3. JUnit 5
  4. JDK 8 or late
assertNotNull(Object actual) method asserts that actual is not null.

assertNotNull() Method Example

package com.javaguides.junit5.assertions;

import static org.junit.jupiter.api.Assertions.assertNotNull;

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

import org.junit.jupiter.api.Test;

/**
 * JUnit5AssertNotNullExample
 * @author javaguides.net
 *
 */
public class JUnit5AssertNotNullExample {
 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 = toStringArray(Arrays.asList("a", "b", "c"));
  for (final String element : strArray) {
   assertNotNull(element);
  }
 }
}

Output

Comments