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?
Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers ('Hamcrest' is an anagram of 'matchers'), allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock.
Hamcrest is the well-known framework used for unit testing in the Java ecosystem. It's bundled in JUnit and simply put, it uses existing predicates – called matcher classes – for making assertions.
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")));
The Hamcrest Optional from Narendra Pathai does great job indeed.
import static com.github.npathai.hamcrestopt.OptionalMatchers.isEmpty;
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresent;
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAnd;
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAndIs;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@Test
public void testOptionalValue() {
Optional<String> option = Optional.of("value");
assertTrue(option.isPresent()); // the old-fashioned, non-diagnosable assertion
assertThat(option, isPresent());
assertThat(option, isPresentAndIs("value"));
assertThat(option, isPresentAnd(startsWith("v")));
assertThat(option, isEmpty()); // fails
}
The last assertion above fails and produces nice diagnosable message:
java.lang.AssertionError:
Expected: is <Empty>
but: had value "value"
Available on Maven :
<dependency>
<groupId>com.github.npathai</groupId>
<artifactId>hamcrest-optional</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With