An extension to Java Hamcrest which provides matchers for java.util.Optional
.
Changes
- Methods renamed from
hasValue
toisPresentAndIs/isPresentAnd
for better fluency.
<dependencies>
...
<dependency>
<groupId>com.github.npathai</groupId>
<artifactId>hamcrest-optional</artifactId>
<version>2.0.0</version>
</dependency>
...
</dependencies>
<dependencies>
...
<dependency>
<groupId>com.github.npathai</groupId>
<artifactId>hamcrest-optional</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
hamcrest-optional provides four matchers for Optional
: isEmpty()
,
isPresent()
, isPresentAndIs(Object)
and isPresentAnd(Matcher)
.
This matcher matches when the examined Optional
contains no value.
import static com.github.npathai.hamcrestopt.OptionalMatchers.isEmpty;
Optional<Object> optional = Optional.empty();
assertThat(optional, isEmpty());
This matcher matches when the examined Optional
contains a value.
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresent;
Optional<String> optional = Optional.of("dummy value");
assertThat(optional, isPresent());
This matcher matches when the examined Optional
contains a value that is
logically equal to the specified object.
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAndIs;
Optional<String> optional = Optional.of("dummy value");
assertThat(optional, isPresentAndIs("dummy value"));
This matcher matches when the examined Optional
contains a value that
satisfies the specified matcher.
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAnd;
import static org.hamcrest.Matchers.startsWith;
Optional<String> optional = Optional.of("dummy value");
assertThat(optional, isPresentAnd(startsWith("dummy")));
hamcrest-optional is build with Maven. If you want to contribute code then
- Please write a test for your change.
- Ensure that you don't break the build by running
mvn verify -Dgpg.skip
. - Fork the repo and create a pull request. (See Understanding the GitHub Flow)
hamcrest-optional supports Travis CI for continuous integration. Your pull request is automatically build by Travis CI.
- Select a new version according to the Semantic Versioning 2.0.0 Standard.
- Set the new version in
pom.xml
. - Commit the modified
pom.xml
. - Push the commit:
git push origin master
- Run
mvn clean deploy
with JDK 8. - Add a tag for the release:
git tag hamcrest-optional-X.X.X
- Push the tag:
git push origin hamcrest-optional-X.X.X