Is there any Hamcrest Matcher for java.util.Optional?

I am looking for a Matcrest Matcher for unit test methods returning the java.util.Optional type. Sort of:

@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 to drop the Maven repository?

+6
source share
2 answers

Currently, Java Hamcrest uses version 1.6 and is integrated with many projects using an older version of Java.

Thus, features related to Java 8 will be added in future versions compatible with Java 8. The proposed solution was to have an extension library that supports it so that anyone who needs can use the extension library.

I am the author of Hamcrest Optional , and now it is available on the central server Maven.

Example: checking if an optional element contains a line 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"))); 
+5
source

At the moment I have the following information:

  • There is an issue and a feature suggestion to support it using Java 8 types on the hamcrest site.
  • One user created one and posted on his GitHub as an example. Still not on Maven, but working on it.
+2
source

All Articles