Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.

README.md

Hamcrest Optional

Travis CI build Coverage Status Maven Central MIT license

An extension to Java Hamcrest which provides matchers for java.util.Optional.

Maven Dependency

Version 2.0.0 - Latest

Changes

  • Methods renamed from hasValue to isPresentAndIs/isPresentAnd for better fluency.
<dependencies>
...
<dependency>
  <groupId>com.github.npathai</groupId>
  <artifactId>hamcrest-optional</artifactId>
  <version>2.0.0</version>
</dependency>
...
</dependencies>

Version 1.0

<dependencies>
...
<dependency>
  <groupId>com.github.npathai</groupId>
  <artifactId>hamcrest-optional</artifactId>
  <version>1.0</version>
</dependency>
...
</dependencies>

Usage

hamcrest-optional provides four matchers for Optional: isEmpty(), isPresent(), isPresentAndIs(Object) and isPresentAnd(Matcher).

isEmpty()

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());

isPresent()

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());

isPresentAndIs(Object)

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"));

isPresentAnd(Matcher)

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")));

Development Guide

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.

Release Guide

  • 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
You can’t perform that action at this time.