https://github.com/npathai/hamcrest-optional
Matchers for JDK 8 Optional
https://github.com/npathai/hamcrest-optional
assertion-library assertions hamcrest java-8 junit4 junit5 matcher optional
Last synced: 25 days ago
JSON representation
Matchers for JDK 8 Optional
- Host: GitHub
- URL: https://github.com/npathai/hamcrest-optional
- Owner: npathai
- License: mit
- Created: 2015-07-08T07:08:00.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-06-03T15:34:52.000Z (over 3 years ago)
- Last Synced: 2023-10-30T11:52:54.038Z (over 2 years ago)
- Topics: assertion-library, assertions, hamcrest, java-8, junit4, junit5, matcher, optional
- Language: Java
- Homepage:
- Size: 44.9 KB
- Stars: 43
- Watchers: 5
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: License.txt
Awesome Lists containing this project
README
# Hamcrest Optional
[](https://travis-ci.org/npathai/hamcrest-optional) [](https://coveralls.io/github/npathai/hamcrest-optional?branch=master)
[](https://maven-badges.herokuapp.com/maven-central/com.github.npathai/hamcrest-optional)
[](https://github.com/npathai/hamcrest-optional/blob/master/License.txt)
An extension to [Java Hamcrest](https://github.com/hamcrest/JavaHamcrest) 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.
```
...
com.github.npathai
hamcrest-optional
2.0.0
...
```
### Version 1.0
```
...
com.github.npathai
hamcrest-optional
1.0
...
```
## 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.
```java
import static com.github.npathai.hamcrestopt.OptionalMatchers.isEmpty;
Optional optional = Optional.empty();
assertThat(optional, isEmpty());
```
### isPresent()
This matcher matches when the examined `Optional` contains a value.
```java
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresent;
Optional 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.
```java
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAndIs;
Optional 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.
```java
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAnd;
import static org.hamcrest.Matchers.startsWith;
Optional optional = Optional.of("dummy value");
assertThat(optional, isPresentAnd(startsWith("dummy")));
```
## Development Guide
hamcrest-optional is build with [Maven](http://maven.apache.org/). 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](https://guides.github.com/introduction/flow/index.html))
hamcrest-optional supports [Travis CI](https://travis-ci.org/) 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](http://semver.org/).
* 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`