I am looking for a Hamcrest Matcher to unit test methods that return a java.util.Optional type. Something like:
@Test
public void get__Null(){
Optional<Element> element = Element.get(null);
assertThat( sasi , isEmptyOptional());
}
@Test
public void get__GetCode(){
Optional<Element> element = Element.get(MI_CODE);
assertThat( sasi , isOptionalThatMatches(allOf(hasproperty("code", MI_CODE),
hasProperty("id", notNullValue())));
}
Is there any implementation available throw the Maven Repository?
Presently Java Hamcrest is using 1.6 version and is integrated with many projects that use older version of Java.
So the features related to Java 8 will be added in future versions that are Java 8 compatible. The solution proposed was to have an extension library that supports it, so that anyone who needs can use extension library.
I am the author of Hamcrest Optional and it is now available on Maven central.
Example: Checking if the Optional contains a string starting with some value
import static com.github.npathai.hamcrestopt.OptionalMatchers.hasValue;
import static org.hamcrest.Matchers.startsWith;
Optional<String> optional = Optional.of("dummy value");
assertThat(optional, hasValue(startsWith("dummy")));