public abstract class BeanMatchers extends Object
Matcher instances for testing properties of objects whose properties follow the Java beans standard| Constructor and Description |
|---|
BeanMatchers() |
| Modifier and Type | Method and Description |
|---|---|
static <T> org.hamcrest.Matcher<T> |
hasPath(String name,
org.hamcrest.Matcher<?> matcher)
Return an instance of a
Matcher which will test if an object has a named path with the given value. |
static <T> org.hamcrest.Matcher<T> |
hasProperty(String name,
org.hamcrest.Matcher<?> matcher)
Return an instance of a
Matcher which will test if an object has a named property with the given value. |
static <T> TheSameAs<T> |
theSameAs(T object)
Return an instance of a
Matcher which will perform a deep comparison of the two objects. |
static <T> TheSameAs<T> |
theSameAs(T object,
String name)
Return an instance of a
Matcher which will perform a deep comparison of the two objects. |
public static <T> TheSameAs<T> theSameAs(T object)
Matcher which will perform a deep comparison of the two objects. For Example
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;};
}
// To test a simple object
Person expected = new Person("John", "Doe");
assertThat(new Person("John", "Doe"), theSameAs(expected))
// To test a simple object but ignore differences in a property
Person expected = new Person("Jane", "Doe");
assertThat(new Person("John", "Doe"), theSameAs(expected).excludeProperty("FirstName"));
T - the type of the instanceobject - the instance to match againstpublic static <T> TheSameAs<T> theSameAs(T object, String name)
Matcher which will perform a deep comparison of the two objects. For Example
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;};
}
// To test a simple object
Person expected = new Person("John", "Doe");
assertThat(new Person("John", "Doe"), theSameAs(expected, "Person"))
// To test a simple object but ignore differences in a property
Person expected = new Person("Jane", "Doe");
assertThat(new Person("John", "Doe"), theSameAs(expected,"Person").excludePath("Person.FirstName"));
T - the type of the instanceobject - the instance to match againstname - the name to use for the base object for paths e.g Person would prefix path i.e. Person.FirstNamepublic static <T> org.hamcrest.Matcher<T> hasProperty(String name, org.hamcrest.Matcher<?> matcher)
Matcher which will test if an object has a named property with the given value. For Example
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;};
}
// To test a simple object
Person expected = new Person("John", "Doe");
assertThat(new Person("John", "Doe"), hasProperty("FirstName, equalTo("John")))
T - the type of the instancename - the name of the propertymatcher - the matcher to test the value of the propertypublic static <T> org.hamcrest.Matcher<T> hasPath(String name, org.hamcrest.Matcher<?> matcher)
Matcher which will test if an object has a named path with the given value. For Example
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;};
}
// To test a simple object
Person expected = new Person("John", "Doe");
assertThat(new Person("John", "Doe"), hasProperty("Person.FirstName, equalTo("John")))
T - the type of the instancepath - the name of the pathmatcher - the matcher to test the value of the pathCopyright © 2015. All rights reserved.