Skip to content

Instantly share code, notes, and snippets.

@awgtek
Created October 19, 2015 21:09
Show Gist options
  • Save awgtek/3bfa25eb8ee050b6770f to your computer and use it in GitHub Desktop.
Save awgtek/3bfa25eb8ee050b6770f to your computer and use it in GitHub Desktop.
Hamcrest wrapper for EqualsBuilder.reflectionEquals
import java.util.Collection;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
/**
* Hamcrest matcher that verifies that is used to assert equality
* based on all fields of the class except for those excluded.
*/
public class BeanFieldsMatcher extends TypeSafeMatcher<Object> {
private Object expected;
private Collection<String> excludeFields;
public BeanFieldsMatcher(Object expected) {
this.expected = expected;
}
@Override
public void describeTo(Description description) {
description.appendValue(expected);
}
@Override
protected boolean matchesSafely(Object item) {
return EqualsBuilder.reflectionEquals(expected, item, excludeFields);
}
public static BeanFieldsMatcher baseEquals(Object expected) {
return new BeanFieldsMatcher(expected);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment