public class OptionalMatchers extends Object
Matcher implementations for Jdk 8 optionals.
Example:
assertThat(Optional.empty(), isEmpty()); assertThat(Optional.of(1), isPresent()); assertThat(Optional.of(1), hasValueThatIs(1)); assertThat(Optional.of(1), hasValueThat(greaterThan(0)));The failure messages have the form
Expected: Optional that is empty
but: Optional was present with value <1>
Expected: Optional that is present
but: Optional was empty
Expected: Optional that is present and has value that is <1>
but: Optional was empty
Expected: Optional that is present and has value that is <1>
but: was present but value was <2>
Expected: Optional that is present and has value that a value greater than <2>
but Optional was present but value <1> was less than <2>
| Constructor and Description |
|---|
OptionalMatchers() |
| Modifier and Type | Method and Description |
|---|---|
static <T> org.hamcrest.Matcher<Optional<T>> |
hasValueThat(org.hamcrest.Matcher<? super T> valueMatcher)
Match a non-empty
Optional whose value matches a given matcher. |
static <T> org.hamcrest.Matcher<Optional<T>> |
hasValueThatIs(T value)
Match a non-empty
Optional whose value is equal to a given value. |
static <T> org.hamcrest.Matcher<Optional<T>> |
isEmpty()
Match an
Optional exactly when it is empty. |
static <T> org.hamcrest.Matcher<Optional<T>> |
isPresent()
Match an
Optional exactly when it is present. |
public static <T> org.hamcrest.Matcher<Optional<T>> isEmpty()
Optional exactly when it is empty.Matcher which matches an Optional if and only if Optional.isPresent() is false.public static <T> org.hamcrest.Matcher<Optional<T>> isPresent()
Optional exactly when it is present.Matcher which matches an Optional if and only if Optional.isPresent() is true.public static <T> org.hamcrest.Matcher<Optional<T>> hasValueThatIs(T value)
Optional whose value is equal to a given value.T - the type of the value in the Optional.value - the expected value contained in the Optional.Matcher which matches an Optional if and only if Optional.isPresent() is true and
Optional.get() returns a value which is the given value.public static <T> org.hamcrest.Matcher<Optional<T>> hasValueThat(org.hamcrest.Matcher<? super T> valueMatcher)
Optional whose value matches a given matcher.T - the type of the value in the Optional.valueMatcher - the matcher for the expected value contained in the Optional.Matcher which matches an Optional if and only if Optional.isPresent() is true and
Optional.get() returns a value which valueMatcher matches.Copyright © 2017 Atlassian. All rights reserved.