Skip to content

Instantly share code, notes, and snippets.

@sakamotodesu
Created August 20, 2013 00:11
Show Gist options
  • Save sakamotodesu/6275662 to your computer and use it in GitHub Desktop.
Save sakamotodesu/6275662 to your computer and use it in GitHub Desktop.
hamcrest customized matcher
public class FileExist extends TypeSafeMatcher<File> {
private final boolean isEexpectedResult;
private File inspected;
public FileExist(boolean exceptedResult) {
this.isEexpectedResult = exceptedResult;
}
@Override
public void describeTo(Description description) {
description.appendText(inspected.getAbsolutePath());
if (isEexpectedResult) {
description.appendText(" exist");
} else {
description.appendText(" does not exist");
}
}
@Override
protected void describeMismatchSafely(File item, Description mismatchDescription) {
mismatchDescription.appendText(inspected.getName());
if (isEexpectedResult) {
mismatchDescription.appendText(" does not exist.");
} else {
mismatchDescription.appendText(" exist.");
}
}
@Override
protected boolean matchesSafely(File item) {
this.inspected = item;
return item.exists() == isEexpectedResult;
}
@Factory
public static <T> Matcher<File> exist() {
return new FileExist(true);
}
@Factory
public static <T> Matcher<File> NotExist() {
return new FileExist(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment