Skip to content

Instantly share code, notes, and snippets.

@xsveda
Created March 17, 2016 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xsveda/37d3fb8b87f618833923 to your computer and use it in GitHub Desktop.
Save xsveda/37d3fb8b87f618833923 to your computer and use it in GitHub Desktop.
Hamcrest matchers for Optional class
public class OptionalMatcher {
public static IsPresentMatcher isPresent() {
return new IsPresentMatcher();
}
public static IsEmptyMatcher isEmpty() {
return new IsEmptyMatcher();
}
public static class IsPresentMatcher extends TypeSafeDiagnosingMatcher<Optional> {
@Override
protected boolean matchesSafely(Optional optional, Description description) {
return optional.isPresent();
}
@Override
public void describeTo(Description description) {
description.appendText("Optional value should be present");
}
}
public static class IsEmptyMatcher extends TypeSafeDiagnosingMatcher<Optional> {
@Override
protected boolean matchesSafely(Optional optional, Description description) {
return !optional.isPresent();
}
@Override
public void describeTo(Description description) {
description.appendText("Optional value should be empty");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment