Assert Optional Value in Java | ninjasquad
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:-
-
Test that a value is present in Optional:-
Optional<String> optional = Optional.of("foo"); assertTrue(optional.isPresent()); // pass
-
Test that a value is not present or empty in Optional:-
Optional<String> optional = Optional.empty(); assertFalse(optional.isPresent()); // pass
-
Test an Optional String:-
Optional<String> optional = Optional.of("foo"); assertTrue(optional.isPresent()); assertEquals("foo", optional.get());
-
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.
AssertJ
AssertJ has good support for Optional objects and provides many fluent assertions specific to Optional. Let’s look at the below examples:-
-
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()
andisNotEmpty()
are alias and can be used interchangeably. -
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()
andisNotPresent()
are alias and anyone can be used interchangeably. -
Test an Optional String:-
Optional<String> optional = Optional.of("foo"); assertThat(optional) .isPresent() .isNotEmpty() .containsInstanceOf(String.class) .hasValue("foo") .contains("foo");
Note that
hasValue()
andcontains()
are alias and anyone can be used interchangeably. -
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.
Source: Internet