Created
October 19, 2015 21:09
-
-
Save awgtek/3bfa25eb8ee050b6770f to your computer and use it in GitHub Desktop.
Hamcrest wrapper for EqualsBuilder.reflectionEquals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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