public class OptionalMatchers extends Object
java.util.Optional:
isEmpty() - matches when the examined Optional
contains no value.isPresent() - matches when the examined Optional
contains a value.hasValue(Object) - matches when the examined
Optional contains a value that is logically equal to the
operand.hasValue(Matcher) - matches when the examined
Optional contains a value that satisfies the specified matcher.
| Modifier and Type | Method and Description |
|---|---|
static <T> org.hamcrest.Matcher<Optional<T>> |
hasValue(org.hamcrest.Matcher<? super T> matcher)
Creates a matcher that matches when the examined
Optional
contains a value that satisfies the specified matcher. |
static <T> org.hamcrest.Matcher<Optional<T>> |
hasValue(T operand)
Creates a matcher that matches when the examined
Optional
contains a value that is logically equal to the operand, as
determined by calling the equals method on the value. |
static org.hamcrest.Matcher<Optional<?>> |
isEmpty()
Creates a matcher that matches when the examined
Optional
contains no value. |
static org.hamcrest.Matcher<Optional<?>> |
isPresent()
Creates a matcher that matches when the examined
Optional
contains a value. |
public static org.hamcrest.Matcher<Optional<?>> isPresent()
Optional
contains a value.
Optional<String> optionalObject = Optional.of("dummy value");
assertThat(optionalObject, isPresent());
Optional
contains a value.public static org.hamcrest.Matcher<Optional<?>> isEmpty()
Optional
contains no value.
Optional<String> optionalObject = Optional.empty();
assertThat(optionalObject, isEmpty());
Optional
contains no value.public static <T> org.hamcrest.Matcher<Optional<T>> hasValue(T operand)
Optional
contains a value that is logically equal to the operand, as
determined by calling the equals method on the value.
Optional<String> optionalInt = Optional.of("dummy value");
assertThat(optionalInt, hasValue("dummy value"));
T - the class of the value.operand - the object that any examined Optional value
should equalOptional
contains a value that is logically equal to the operand.public static <T> org.hamcrest.Matcher<Optional<T>> hasValue(org.hamcrest.Matcher<? super T> matcher)
Optional
contains a value that satisfies the specified matcher.
Optional<String> optionalObject = Optional.of("dummy value");
assertThat(optionalObject, hasValue(startsWith("dummy")));
T - the class of the value.matcher - a matcher for the value of the examined Optional.Optional
contains a value that satisfies the specified matcher.Copyright © 2015–2016. All rights reserved.