Hamcrest Optional

A set of Hamcrest matchers for java.util.Optional.

License

License

GroupId

GroupId

com.github.npathai
ArtifactId

ArtifactId

hamcrest-optional
Last Version

Last Version

2.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Hamcrest Optional
A set of Hamcrest matchers for java.util.Optional.
Project URL

Project URL

https://github.com/npathai/hamcrest-optional/
Source Code Management

Source Code Management

https://github.com/npathai/hamcrest-optional/

Download hamcrest-optional

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.npathai/hamcrest-optional/ -->
<dependency>
    <groupId>com.github.npathai</groupId>
    <artifactId>hamcrest-optional</artifactId>
    <version>2.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.npathai/hamcrest-optional/
implementation 'com.github.npathai:hamcrest-optional:2.0.0'
// https://jarcasting.com/artifacts/com.github.npathai/hamcrest-optional/
implementation ("com.github.npathai:hamcrest-optional:2.0.0")
'com.github.npathai:hamcrest-optional:jar:2.0.0'
<dependency org="com.github.npathai" name="hamcrest-optional" rev="2.0.0">
  <artifact name="hamcrest-optional" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.npathai', module='hamcrest-optional', version='2.0.0')
)
libraryDependencies += "com.github.npathai" % "hamcrest-optional" % "2.0.0"
[com.github.npathai/hamcrest-optional "2.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.hamcrest : hamcrest-core jar 1.3

Project Modules

There are no modules declared in this project.

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

Versions

Version
2.0.0
1.0