public class TheSameAs<T>
extends org.hamcrest.TypeSafeDiagnosingMatcher<T>
Matcher for performing a deep comparison of two objects by testing getters which start with get, is, or has are the same
on each instances.
When comparing elements in a collection or an array the Matcher orders a copy of the collection. If there is a default comparator then one will be used, or alternatively
one will built using CompareToBuilder.reflectionCompare(Object, Object)
| Modifier and Type | Class and Description |
|---|---|
static interface |
TheSameAs.PropertyComparator
Interface to be implemented by classes which can compare two property values to confirm if they're equivalent
|
| Constructor and Description |
|---|
TheSameAs(T object) |
TheSameAs(T object,
String name) |
| Modifier and Type | Method and Description |
|---|---|
TheSameAs<T> |
comparePath(String path,
TheSameAs.PropertyComparator comparator)
Override the PropertyComparator used for a path.
|
TheSameAs<T> |
compareProperty(String path,
TheSameAs.PropertyComparator comparator)
Override the PropertyComparator used for a property.
|
TheSameAs<T> |
compareType(Class<?> type,
TheSameAs.PropertyComparator comparator)
Override the PropertyComparator used for a type.
|
void |
describeTo(org.hamcrest.Description description) |
TheSameAs<T> |
excludePath(String path)
Exclude a property path from the comparison.
|
TheSameAs<T> |
excludeProperty(String property)
Exclude a property from the comparison.
|
TheSameAs<T> |
excludeType(Class<?> type)
Exclude a type from the comparison.
|
protected boolean |
matchesSafely(T item,
org.hamcrest.Description mismatchDesc) |
static <T> TheSameAs<T> |
theSameAs(T object)
Creates a matcher that matches the full object graph for the given instance against another instance
For example:
|
static <T> TheSameAs<T> |
theSameAs(T object,
String name)
Creates a matcher that matches the full object graph for the given instance against another instance
For example:
|
public TheSameAs(T object)
public static <T> TheSameAs<T> theSameAs(T object)
MyObject instance = new MyObject(); dao.save(instance); // Save instance to persistent store assertThat(dao.getById(instance.getId()), theSameAs(instance);
object - the instance to match againstpublic static <T> TheSameAs<T> theSameAs(T object, String name)
MyObject instance = new MyObject(); dao.save(instance); // Save instance to persistent store assertThat(dao.getById(instance.getId()), theSameAs(instance, "MyInstance");
object - the instance to match againstname - the name given to the root entitypublic TheSameAs<T> excludePath(String path)
class Person [
private String firstName, lastName;
public Person(final String firstName, final String lastName) {
this.firstname = firstName;
this.lastName = lastName;
}
public String getFirstName() { return firstName;};
public String getLastName() { return lastName;};
}
// Testing a simple object is the same except for a property
Person expected = new Person("Jane", "Doe");
MatcherAssert.assertThat(new Person("John", "Doe"), BeanMatchers.theSameAs(expected).excludePath("Person.LastName"));
property - the path to exclude from the comparison e.g Person.LastNamepublic TheSameAs<T> excludeProperty(String property)
class Person [
private String firstName, lastName;
public Person(final String firstName, final String lastName) {
this.firstname = firstName;
this.lastName = lastName;
}
public String getFirstName() { return firstName;};
public String getLastName() { return lastName;};
}
// Testing a simple object is the same except for a property
Person expected = new Person("Jane", "Doe");
MatcherAssert.assertThat(new Person("John", "Doe"), BeanMatchers.theSameAs(expected).excludeProperty("LastName"));
property - the property to exclude from the comparison e.g LastNamepublic TheSameAs<T> excludeType(Class<?> type)
class Person [
private String firstName, lastName;
public Person(final String firstName, final String lastName) {
this.firstname = firstName;
this.lastName = lastName;
}
public String getFirstName() { return firstName;};
public String getLastName() { return lastName;};
}
// Testing a simple object is the same except for a property
Person expected = new Person("Jane", "Doe");
MatcherAssert.assertThat(new Person("John", "Doe"), BeanMatchers.theSameAs(expected).excludeType(String.class));
type - the type to exclude from the comparison e.g String.classpublic TheSameAs<T> comparePath(String path, TheSameAs.PropertyComparator comparator)
class Person [
private String firstName, lastName;
public Person(final String firstName, final String lastName) {
this.firstname = firstName;
this.lastName = lastName;
}
public String getFirstName() { return firstName;};
public String getLastName() { return lastName;};
}
// Testing a simple object is the same except for a property
Person expected = new Person("John", "Doe");
MatcherAssert.assertThat(new Person("john", "doe"), BeanMatchers.theSameAs(expected).comparePath("Person.LastName", new IsEqualsIgnoreCase());
property - the property to exclude from the comparison e.g LastNamepublic TheSameAs<T> compareProperty(String path, TheSameAs.PropertyComparator comparator)
class Person [
private String firstName, lastName;
public Person(final String firstName, final String lastName) {
this.firstname = firstName;
this.lastName = lastName;
}
public String getFirstName() { return firstName;};
public String getLastName() { return lastName;};
}
// Testing a simple object is the same except for a property
Person expected = new Person("John", "Doe");
MatcherAssert.assertThat(new Person("john", "doe"), BeanMatchers.theSameAs(expected).comparePath("Person.LastName", new IsEqualsIgnoreCase());
path - the path to set the comparator forcomparator - the comparator to usepublic TheSameAs<T> compareType(Class<?> type, TheSameAs.PropertyComparator comparator)
class Person [
private String firstName, lastName;
public Person(final String firstName, final String lastName) {
this.firstname = firstName;
this.lastName = lastName;
}
public String getFirstName() { return firstName;};
public String getLastName() { return lastName;};
}
// Testing a simple object is the same except for a property
Person expected = new Person("John", "Doe");
MatcherAssert.assertThat(new Person("john", "doe"), BeanMatchers.theSameAs(expected).compareType(String.class, new IsEqualsIgnoreCase());
type - the type to set the comparator forcomparator - the comparator to useprotected boolean matchesSafely(T item, org.hamcrest.Description mismatchDesc)
matchesSafely in class org.hamcrest.TypeSafeDiagnosingMatcher<T>public void describeTo(org.hamcrest.Description description)
Copyright © 2015. All rights reserved.