Assert Optional Value in Java Assert Optional Value in Java

JUnit 5, AssertJ

Page content

In this article, we will learn how to assert Optional in Java using JUnit 5 and AssertJ assertion libraries.

Overview

We usually write a unit test to verify the expected output from a method. When a method returns an Optional object, we have to write test cases to check if the Optional is present, empty or has an expected value. We can do that using Junit 5 and AssertJ libraries.

Assert Optional

Let’s look at quick examples to write test cases for returning Optional Object using Junit 5 and AssertJ libraries.

JUnit 5

JUnit 5 doesn’t provide great support for Optional like AssertJ library though we managed to do all basic Optional assertions in the below examples:-

  1. Test that a value is present in Optional:-

    Optional<String> optional = Optional.of("foo");
    assertTrue(optional.isPresent()); // pass
    
  2. Test that a value is not present or empty in Optional:-

    Optional<String> optional = Optional.empty();
    assertFalse(optional.isPresent()); // pass
    
  3. Test an Optional String:-

    Optional<String> optional = Optional.of("foo");
    assertTrue(optional.isPresent());
    assertEquals("foo", optional.get());
    
  4. Test an Optional Object having multiple properties:-

    Optional<User> userOptional = Optional.of(new User("Jack", 21, true));
    assertTrue(userOptional.isPresent());
    userOptional.ifPresent(user -> {
      assertEquals("Jack", user.getFirstName());
      assertTrue(user.getAge() > 18);
      assertTrue(user.getIsPremiumUser());
    });
    

    Note that Junit 5 doesn’t provide any assertion to test multiple properties of an Optional Object so we made use of Optional.ifPresent() to assert all the properties.

Also Read Unit Test with JUnit 5 in Java for more details.


AssertJ

AssertJ has good support for Optional objects and provides many fluent assertions specific to Optional. Let’s look at the below examples:-

  1. Test that a value is present in Optional:-

    Optional<String> optional = Optional.of("foo");
    assertThat(optional).isPresent(); // pass
    assertThat(optional).isNotEmpty(); // pass
    

    Note that isPresent() and isNotEmpty() are alias and can be used interchangeably.

  2. Test that a value is not present or empty in Optional:-

    Optional<String> optional = Optional.empty();
    assertThat(optional).isEmpty(); // pass
    assertThat(optional).isNotPresent(); // pass
    

    Note that isEmpty() and isNotPresent() are alias and anyone can be used interchangeably.

  3. Test an Optional String:-

    Optional<String> optional = Optional.of("foo");
    assertThat(optional)
            .isPresent()
            .isNotEmpty()
            .containsInstanceOf(String.class)
            .hasValue("foo")
            .contains("foo");
    

    Note that hasValue() and contains() are alias and anyone can be used interchangeably.

  4. Test an Optional Object having multiple properties:-

    Optional<User> userOptional = Optional.of(new User("Jack", 21, true));
    assertThat(userOptional)
            .isPresent()
            .isNotEmpty()
            .containsInstanceOf(User.class)
            .hasValueSatisfying(user -> {
                assertThat(user.getFirstName()).isEqualTo("Jack");
                assertThat(user.getAge()).isGreaterThan(18);
                assertThat(user.getIsPremiumUser()).isTrue();
            });
    

    We have tested all the properties of an Optional Object using hasValueSatisfying() in the above example.

Also Read Unit Test with AssertJ in Java for more details.