[Java] How to test for null with JUnit

background

When writing unit tests for existing source code, it was necessary to perform tests to verify whether they were null as the title suggests. I have briefly summarized what I learned in the process.

The tool and version I'm using is JUnit 4 (.12) and Hamcrest is 1.3.

This time, I will write it on the assumption that I will write a test in the Prefix Adder of ↓.

PrefixAdder.java


public class PrefixAdder {
  public static String execute(String target, String prefix) {
    if (Objects.isNull(target)) {
      return null;
    }
    return prefix + target;
  }
}

Failure example

At first, I intuitively wrote the following as usual, but this way of writing results in a compilation error at a level that warns the IDE.

PrefixAdderTest.java


public class PrefixAdderTest {
  @Before
  public void setUp() {
  }

  @Test
  public void returnNullWhenTargetIsNull() {
    String result = PrefixAdder.execute(null, "prefix");
    //↓ Compile error
    assertThat(result, is(null));
  }
}

Success story

So how to write it is to use the nullValue method of Hamcrest's IsNull class.

PrefixAdderTest.java


public class PrefixAdderTest {
  @Before
  public void setUp() {
  }

  @Test
  public void returnNullWhenTargetIsNull() {
    String result = PrefixAdder.execute(null, "prefix");
    assertThat(result, nullValue());
  }
}

Now you can test for null. If you want to test that it is not null, you can also use the notNullValue method of the IsNull class.

Summary

If you want to write a test that verifies whether a value is null in JUnit, use the nullValue method of Hamcrest's IsNull class. If you use the is method of the Is class in the usual way, a compile error will occur.

It's easy, but I hope this article has helped you a little.

reference

Hamcrest Tutorial Hamcrest GitHub IsNull class

Recommended Posts

[Java] How to test for null with JUnit
How to test private scope with JUnit
How to test interrupts during Thread.sleep with JUnit
[Java] Test private methods with JUnit
How to test a private method with RSpec for yourself
Investigated how to call services with Watson SDK for Java
[Java] I want to test standard input & standard output with JUnit
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
How to filter JUnit Test in Gradle
Java automated test implementation with JUnit 5 + Gradle
[CircleCI 2.0] [Java] [Maven] [JUnit] Aggregate JUnit test results with CircleCI 2.0
Java automated test implementation with JUnit 5 + Apache Maven
How to test private methods with arrays or variadic arguments in JUnit
[Java] How to turn a two-dimensional array with an extended for statement
JUnit 5: How to write test cases in enum
How to use Java framework with AWS Lambda! ??
How to use Java API with lambda expression
How to hide null fields in response in Java
[Java] (for MacOS) How to set the classpath
How to use JUnit 5
How to write test code with Basic authentication
Unit test with Junit.
How to use nginx-ingress-controller with Docker for Mac
[Java] How to make multiple for loops single
How to perform UT with Excel as test data with Spring Boot + JUnit5 + DBUnit
How to call functions in bulk with Java reflection
How to write a unit test for Spring Boot 2
How to build docker environment with Gradle for intelliJ
[Java] How to omit spring constructor injection with Lombok
How to deploy Java to AWS Lambda with Serverless Framework
How to unit test with JVM with source using RxAndroid
How to make Java unit tests (JUnit & Mockito & PowerMock)
How to deal with No template for interactive request
[For beginners] How to operate Stream API after Java 8
[Java] How to encrypt with AES encryption with standard library
How to use Truth (assertion library for Java / Android)
Try connecting to AzureCosmosDB Emulator for Docker with Java
How to build Java development environment with VS Code
How to make Laravel faster with Docker for Mac
How to write modern Java. Immutable Java, consider Null safety.
How to loop Java Map (for Each / extended for statement)
How to execute WebCamCapture sample of NyARToolkit for Java
[Java] How to start a new line with StringBuilder
[Java] How to use Map
[Java] JUnit4 test case example
How to lower java version
[Java] How to use Map
Test Web API with junit
How to uninstall Java 8 (Mac)
Java to play with Function
Java --How to make JTable
How to use java Optional
How to use JUnit (beginner)
How to minimize Java images
How to write java comments
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
2018 Java Proficiency Test for Newcomers-Basics-