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<T>
Interface to be implemented by classes which can compare two property
values to confirm if they're equivalent
|
static class |
TheSameAs.PropertyType
Enumeration of the types of properties the matcher will check.
|
| Constructor and Description |
|---|
TheSameAs(T object) |
TheSameAs(T object,
String name,
TheSameAs.PropertyType propertyTypes) |
TheSameAs(T object,
TheSameAs.PropertyType propertyTypes) |
| Modifier and Type | Method and Description |
|---|---|
<P> TheSameAs<T> |
comparePath(String path,
org.hamcrest.Matcher<P> matcher)
Override the PropertyComparator used for a path to use a hamcrest
Matcher.
|
TheSameAs<T> |
comparePath(String path,
TheSameAs.PropertyComparator<?> comparator)
Override the PropertyComparator used for a path.
|
<P> TheSameAs<T> |
compareProperty(String path,
org.hamcrest.Matcher<P> matcher)
Override the PropertyComparator used for a property to use a hamcrest
matcher.
|
TheSameAs<T> |
compareProperty(String path,
TheSameAs.PropertyComparator<?> comparator)
Override the PropertyComparator used for a property.
|
<P> TheSameAs<T> |
compareType(Class<P> type,
org.hamcrest.Matcher<P> matcher)
Override the PropertyComparator used for a type to use a hamcrest
Matcher.
|
<P> TheSameAs<T> |
compareType(Class<P> type,
TheSameAs.PropertyComparator<P> 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 by comparing all getter properties
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:
|
static <T> TheSameAs<T> |
theSameBeanAs(T object)
Creates a matcher that matches the full object graph for the given
instance against another instance by comparing bean properties i.e.
|
static <T> TheSameAs<T> |
theSameBeanAs(T object,
String name)
Creates a matcher that matches the full object graph for the given
instance against another instance by comparing bean properties i.e.
|
public TheSameAs(T object)
public TheSameAs(T object, TheSameAs.PropertyType propertyTypes)
public TheSameAs(T object, String name, TheSameAs.PropertyType propertyTypes)
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> theSameBeanAs(T object)
MyObject instance = new MyObject(); dao.save(instance); // Save instance to persistent store assertThat(dao.getById(instance.getId()), theSameBeanAs(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 static <T> TheSameAs<T> theSameBeanAs(T object, String name)
MyObject instance = new MyObject(); dao.save(instance); // Save instance to persistent store assertThat(dao.getById(instance.getId()), theSameBeanAs(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 <P> TheSameAs<T> compareType(Class<P> type, TheSameAs.PropertyComparator<P> 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 usepublic <P> TheSameAs<T> comparePath(String path, org.hamcrest.Matcher<P> matcher)
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", "Deer"), BeanMatchers.theSameAs(expected).comparePath("Person.LastName", Matchers.startsWith("D")));
property - the property to exclude from the comparison e.g LastNamepublic <P> TheSameAs<T> compareProperty(String path, org.hamcrest.Matcher<P> matcher)
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", "Deer"), BeanMatchers.theSameAs(expected).comparePath("Person.LastName", Matchers.startsWith("D")));
path - the path to set the comparator formatcher - the matcher to usepublic <P> TheSameAs<T> compareType(Class<P> type, org.hamcrest.Matcher<P> matcher)
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, Matchers.startsWith("J")));
type - the type to set the comparator formatcher - the matcher 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.