public class Matchers
extends java.lang.Object
Constructor and Description |
---|
Matchers() |
Modifier and Type | Method and Description |
---|---|
static <T> Matcher<T> |
allOf(java.lang.Iterable<Matcher<? super T>> matchers)
Creates a matcher that matches if the examined object matches ALL of the specified matchers.
|
static <T> Matcher<T> |
allOf(Matcher<? super T>... matchers)
Creates a matcher that matches if the examined object matches ALL of the specified matchers.
|
static <T> Matcher<T> |
allOf(Matcher<? super T> first,
Matcher<? super T> second)
Creates a matcher that matches if the examined object matches ALL of the specified matchers.
|
static <T> Matcher<T> |
allOf(Matcher<? super T> first,
Matcher<? super T> second,
Matcher<? super T> third)
Creates a matcher that matches if the examined object matches ALL of the specified matchers.
|
static <T> Matcher<T> |
allOf(Matcher<? super T> first,
Matcher<? super T> second,
Matcher<? super T> third,
Matcher<? super T> fourth)
Creates a matcher that matches if the examined object matches ALL of the specified matchers.
|
static <T> Matcher<T> |
allOf(Matcher<? super T> first,
Matcher<? super T> second,
Matcher<? super T> third,
Matcher<? super T> fourth,
Matcher<? super T> fifth)
Creates a matcher that matches if the examined object matches ALL of the specified matchers.
|
static <T> Matcher<T> |
allOf(Matcher<? super T> first,
Matcher<? super T> second,
Matcher<? super T> third,
Matcher<? super T> fourth,
Matcher<? super T> fifth,
Matcher<? super T> sixth)
Creates a matcher that matches if the examined object matches ALL of the specified matchers.
|
static <K,V> Matcher<java.util.Map<? extends K,? extends V>> |
aMapWithSize(int size)
Creates a matcher for
Map s that matches when the size() method returns
a value equal to the specified size . |
static <K,V> Matcher<java.util.Map<? extends K,? extends V>> |
aMapWithSize(Matcher<? super java.lang.Integer> sizeMatcher)
Creates a matcher for
Map s that matches when the size() method returns
a value that satisfies the specified matcher. |
static <K,V> Matcher<java.util.Map<? extends K,? extends V>> |
anEmptyMap()
Creates a matcher for
Map s that matches when the size() method returns
zero. |
static <T> Matcher<T> |
any(java.lang.Class<T> type)
Creates a matcher that matches when the examined object is an instance of the specified
type ,
as determined by calling the Class.isInstance(Object) method on that type, passing the
the examined object. |
static <T> AnyOf<T> |
anyOf(java.lang.Iterable<Matcher<? super T>> matchers)
Creates a matcher that matches if the examined object matches ANY of the specified matchers.
|
static <T> AnyOf<T> |
anyOf(Matcher<? super T>... matchers)
Creates a matcher that matches if the examined object matches ANY of the specified matchers.
|
static <T> AnyOf<T> |
anyOf(Matcher<? super T> first,
Matcher<? super T> second)
Creates a matcher that matches if the examined object matches ANY of the specified matchers.
|
static <T> AnyOf<T> |
anyOf(Matcher<? super T> first,
Matcher<? super T> second,
Matcher<? super T> third)
Creates a matcher that matches if the examined object matches ANY of the specified matchers.
|
static <T> AnyOf<T> |
anyOf(Matcher<? super T> first,
Matcher<? super T> second,
Matcher<? super T> third,
Matcher<? super T> fourth)
Creates a matcher that matches if the examined object matches ANY of the specified matchers.
|
static <T> AnyOf<T> |
anyOf(Matcher<? super T> first,
Matcher<? super T> second,
Matcher<? super T> third,
Matcher<? super T> fourth,
Matcher<? super T> fifth)
Creates a matcher that matches if the examined object matches ANY of the specified matchers.
|
static <T> AnyOf<T> |
anyOf(Matcher<? super T> first,
Matcher<? super T> second,
Matcher<? super T> third,
Matcher<? super T> fourth,
Matcher<? super T> fifth,
Matcher<? super T> sixth)
Creates a matcher that matches if the examined object matches ANY of the specified matchers.
|
static Matcher<java.lang.Object> |
anything()
Creates a matcher that always matches, regardless of the examined object.
|
static Matcher<java.lang.Object> |
anything(java.lang.String description)
Creates a matcher that always matches, regardless of the examined object, but describes
itself with the specified
String . |
static <T> IsArray<T> |
array(Matcher<? super T>... elementMatchers)
Creates a matcher that matches arrays whose elements are satisfied by the specified matchers.
|
static <E> Matcher<E[]> |
arrayContaining(E... items)
Creates a matcher for arrays that matches when each item in the examined array is
logically equal to the corresponding item in the specified items.
|
static <E> Matcher<E[]> |
arrayContaining(java.util.List<Matcher<? super E>> itemMatchers)
Creates a matcher for arrays that matches when each item in the examined array satisfies the
corresponding matcher in the specified list of matchers.
|
static <E> Matcher<E[]> |
arrayContaining(Matcher<? super E>... itemMatchers)
Creates a matcher for arrays that matches when each item in the examined array satisfies the
corresponding matcher in the specified matchers.
|
static <E> Matcher<E[]> |
arrayContainingInAnyOrder(java.util.Collection<Matcher<? super E>> itemMatchers)
Creates an order agnostic matcher for arrays that matches when each item in the
examined array satisfies one matcher anywhere in the specified collection of matchers.
|
static <E> Matcher<E[]> |
arrayContainingInAnyOrder(E... items)
Creates an order agnostic matcher for arrays that matches when each item in the
examined array is logically equal to one item anywhere in the specified items.
|
static <E> Matcher<E[]> |
arrayContainingInAnyOrder(Matcher<? super E>... itemMatchers)
Creates an order agnostic matcher for arrays that matches when each item in the
examined array satisfies one matcher anywhere in the specified matchers.
|
static <E> Matcher<E[]> |
arrayWithSize(int size)
Creates a matcher for arrays that matches when the
length of the array
equals the specified size . |
static <E> Matcher<E[]> |
arrayWithSize(Matcher<? super java.lang.Integer> sizeMatcher)
Creates a matcher for arrays that matches when the
length of the array
satisfies the specified matcher. |
static Matcher<java.lang.String> |
blankOrNullString()
Creates a matcher of
String that matches when the examined string is null , or
contains zero or more whitespace characters and nothing else. |
static Matcher<java.lang.String> |
blankString()
Creates a matcher of
String that matches when the examined string contains
zero or more whitespace characters and nothing else. |
static <LHS> CombinableMatcher.CombinableBothMatcher<LHS> |
both(Matcher<? super LHS> matcher)
Creates a matcher that matches when both of the specified matchers match the examined object.
|
static Matcher<java.math.BigDecimal> |
closeTo(java.math.BigDecimal operand,
java.math.BigDecimal error)
Creates a matcher of
BigDecimal s that matches when an examined BigDecimal is equal
to the specified operand , within a range of +/- error . |
static Matcher<java.lang.Double> |
closeTo(double operand,
double error)
Creates a matcher of
Double s that matches when an examined double is equal
to the specified operand , within a range of +/- error . |
static <T extends java.lang.Comparable<T>> |
comparesEqualTo(T value)
Creates a matcher of
Comparable object that matches when the examined object is
equal to the specified value, as reported by the compareTo method of the
examined object. |
static <E> Matcher<java.lang.Iterable<? extends E>> |
contains(E... items)
Creates a matcher for
Iterable s that matches when a single pass over the
examined Iterable yields a series of items, each logically equal to the
corresponding item in the specified items. |
static <E> Matcher<java.lang.Iterable<? extends E>> |
contains(java.util.List<Matcher<? super E>> itemMatchers)
Creates a matcher for
Iterable s that matches when a single pass over the
examined Iterable yields a series of items, each satisfying the corresponding
matcher in the specified list of matchers. |
static <E> Matcher<java.lang.Iterable<? extends E>> |
contains(Matcher<? super E>... itemMatchers)
Creates a matcher for
Iterable s that matches when a single pass over the
examined Iterable yields a series of items, each satisfying the corresponding
matcher in the specified matchers. |
static <E> Matcher<java.lang.Iterable<? extends E>> |
contains(Matcher<? super E> itemMatcher)
Creates a matcher for
Iterable s that matches when a single pass over the
examined Iterable yields a single item that satisfies the specified matcher. |
static <T> Matcher<java.lang.Iterable<? extends T>> |
containsInAnyOrder(java.util.Collection<Matcher<? super T>> itemMatchers)
Creates an order agnostic matcher for
Iterable s that matches when a single pass over
the examined Iterable yields a series of items, each satisfying one matcher anywhere
in the specified collection of matchers. |
static <T> Matcher<java.lang.Iterable<? extends T>> |
containsInAnyOrder(Matcher<? super T>... itemMatchers)
Creates an order agnostic matcher for
Iterable s that matches when a single pass over
the examined Iterable yields a series of items, each satisfying one matcher anywhere
in the specified matchers. |
static <T> Matcher<java.lang.Iterable<? extends T>> |
containsInAnyOrder(T... items)
Creates an order agnostic matcher for
Iterable s that matches when a single pass over
the examined Iterable yields a series of items, each logically equal to one item
anywhere in the specified items. |
static <E> Matcher<java.lang.Iterable<? extends E>> |
containsInRelativeOrder(E... items)
Creates a matcher for
Iterable s that matches when a single pass over the
examined Iterable yields a series of items, that contains items logically equal to the
corresponding item in the specified items, in the same relative order
For example: |
static <E> Matcher<java.lang.Iterable<? extends E>> |
containsInRelativeOrder(java.util.List<Matcher<? super E>> itemMatchers)
Creates a matcher for
Iterable s that matches when a single pass over the
examined Iterable yields a series of items, that contains items satisfying the corresponding
matcher in the specified list of matchers, in the same relative order. |
static <E> Matcher<java.lang.Iterable<? extends E>> |
containsInRelativeOrder(Matcher<? super E>... itemMatchers)
Creates a matcher for
Iterable s that matches when a single pass over the
examined Iterable yields a series of items, that each satisfying the corresponding
matcher in the specified matchers, in the same relative order. |
static Matcher<java.lang.String> |
containsString(java.lang.String substring)
Creates a matcher that matches if the examined
String contains the specified
String anywhere. |
static Matcher<java.lang.String> |
containsStringIgnoringCase(java.lang.String substring)
Creates a matcher that matches if the examined
String contains the specified
String anywhere, ignoring case. |
static <T> Matcher<T> |
describedAs(java.lang.String description,
Matcher<T> matcher,
java.lang.Object... values)
Wraps an existing matcher, overriding its description with that specified.
|
static <LHS> CombinableMatcher.CombinableEitherMatcher<LHS> |
either(Matcher<? super LHS> matcher)
Creates a matcher that matches when either of the specified matchers match the examined object.
|
static <E> Matcher<java.util.Collection<? extends E>> |
empty()
Creates a matcher for
Collection s matching examined collections whose isEmpty
method returns true . |
static <E> Matcher<E[]> |
emptyArray()
Creates a matcher for arrays that matches when the
length of the array
is zero. |
static <E> Matcher<java.util.Collection<E>> |
emptyCollectionOf(java.lang.Class<E> unusedToForceReturnType)
Creates a matcher for
Collection s matching examined collections whose isEmpty
method returns true . |
static <E> Matcher<java.lang.Iterable<? extends E>> |
emptyIterable()
Creates a matcher for
Iterable s matching examined iterables that yield no items. |
static <E> Matcher<java.lang.Iterable<E>> |
emptyIterableOf(java.lang.Class<E> unusedToForceReturnType)
Creates a matcher for
Iterable s matching examined iterables that yield no items. |
static Matcher<java.lang.String> |
emptyOrNullString()
Creates a matcher of
String that matches when the examined string is null , or
has zero length. |
static Matcher<java.lang.String> |
emptyString()
Creates a matcher of
String that matches when the examined string has zero length. |
static Matcher<java.lang.String> |
endsWith(java.lang.String suffix)
Creates a matcher that matches if the examined
String ends with the specified
String . |
static Matcher<java.lang.String> |
endsWithIgnoringCase(java.lang.String suffix)
Creates a matcher that matches if the examined
String ends with the specified
String , ignoring case. |
static <T> Matcher<T> |
equalTo(T operand)
Creates a matcher that matches when the examined object is logically equal to the specified
operand , as determined by calling the Object.equals(java.lang.Object) method on
the examined object. |
static Matcher<java.lang.String> |
equalToCompressingWhiteSpace(java.lang.String expectedString)
Creates a matcher of
String that matches when the examined string is equal to
the specified expectedString, when whitespace differences are (mostly) ignored. |
static Matcher<java.lang.String> |
equalToIgnoringCase(java.lang.String expectedString)
Creates a matcher of
String that matches when the examined string is equal to
the specified expectedString, ignoring case. |
static Matcher<java.lang.String> |
equalToIgnoringWhiteSpace(java.lang.String expectedString)
Deprecated.
|
static Matcher<java.lang.Object> |
equalToObject(java.lang.Object operand)
Creates an
IsEqual matcher that does not enforce the values being
compared to be of the same static type. |
static Matcher<java.util.EventObject> |
eventFrom(java.lang.Class<? extends java.util.EventObject> eventClass,
java.lang.Object source)
Creates a matcher of
EventObject that matches any object
derived from eventClass announced by source. |
static Matcher<java.util.EventObject> |
eventFrom(java.lang.Object source)
Creates a matcher of
EventObject that matches any EventObject
announced by source. |
static <U> Matcher<java.lang.Iterable<? extends U>> |
everyItem(Matcher<U> itemMatcher)
Creates a matcher for
Iterable s that only matches when a single pass over the
examined Iterable yields items that are all matched by the specified
itemMatcher . |
static <T extends java.lang.Comparable<T>> |
greaterThan(T value)
Creates a matcher of
Comparable object that matches when the examined object is
greater than the specified value, as reported by the compareTo method of the
examined object. |
static <T extends java.lang.Comparable<T>> |
greaterThanOrEqualTo(T value)
Creates a matcher of
Comparable object that matches when the examined object is
greater than or equal to the specified value, as reported by the compareTo method
of the examined object. |
static <K,V> Matcher<java.util.Map<? extends K,? extends V>> |
hasEntry(K key,
V value)
Creates a matcher for
Map s matching when the examined Map contains
at least one entry whose key equals the specified key and whose value equals the
specified value . |
static <K,V> Matcher<java.util.Map<? extends K,? extends V>> |
hasEntry(Matcher<? super K> keyMatcher,
Matcher<? super V> valueMatcher)
Creates a matcher for
Map s matching when the examined Map contains
at least one entry whose key satisfies the specified keyMatcher and whose
value satisfies the specified valueMatcher . |
static <T> Matcher<java.lang.Iterable<? super T>> |
hasItem(Matcher<? super T> itemMatcher)
Creates a matcher for
Iterable s that only matches when a single pass over the
examined Iterable yields at least one item that is matched by the specified
itemMatcher . |
static <T> Matcher<java.lang.Iterable<? super T>> |
hasItem(T item)
Creates a matcher for
Iterable s that only matches when a single pass over the
examined Iterable yields at least one item that is equal to the specified
item . |
static <T> Matcher<T[]> |
hasItemInArray(Matcher<? super T> elementMatcher)
Creates a matcher for arrays that matches when the examined array contains at least one item
that is matched by the specified
elementMatcher . |
static <T> Matcher<T[]> |
hasItemInArray(T element)
A shortcut to the frequently used
hasItemInArray(equalTo(x)) . |
static <T> Matcher<java.lang.Iterable<T>> |
hasItems(Matcher<? super T>... itemMatchers)
Creates a matcher for
Iterable s that matches when consecutive passes over the
examined Iterable yield at least one item that is matched by the corresponding
matcher from the specified itemMatchers . |
static <T> Matcher<java.lang.Iterable<T>> |
hasItems(T... items)
Creates a matcher for
Iterable s that matches when consecutive passes over the
examined Iterable yield at least one item that is equal to the corresponding
item from the specified items . |
static <K> Matcher<java.util.Map<? extends K,?>> |
hasKey(K key)
Creates a matcher for
Map s matching when the examined Map contains
at least one key that is equal to the specified key. |
static <K> Matcher<java.util.Map<? extends K,?>> |
hasKey(Matcher<? super K> keyMatcher)
Creates a matcher for
Map s matching when the examined Map contains
at least one key that satisfies the specified matcher. |
static Matcher<java.lang.CharSequence> |
hasLength(int length)
Creates a matcher of
CharSequence that matches when a char sequence has the length
of the specified argument . |
static <T> Matcher<T> |
hasProperty(java.lang.String propertyName)
Creates a matcher that matches when the examined object has a JavaBean property
with the specified name.
|
static <T> Matcher<T> |
hasProperty(java.lang.String propertyName,
Matcher<?> valueMatcher)
Creates a matcher that matches when the examined object has a JavaBean property
with the specified name whose value satisfies the specified matcher.
|
static <E> Matcher<java.util.Collection<? extends E>> |
hasSize(int size)
Creates a matcher for
Collection s that matches when the size() method returns
a value equal to the specified size . |
static <E> Matcher<java.util.Collection<? extends E>> |
hasSize(Matcher<? super java.lang.Integer> sizeMatcher)
Creates a matcher for
Collection s that matches when the size() method returns
a value that satisfies the specified matcher. |
static <T> Matcher<T> |
hasToString(Matcher<? super java.lang.String> toStringMatcher)
Creates a matcher that matches any examined object whose
toString method
returns a value that satisfies the specified matcher. |
static <T> Matcher<T> |
hasToString(java.lang.String expectedToString)
Creates a matcher that matches any examined object whose
toString method
returns a value equalTo the specified string. |
static <V> Matcher<java.util.Map<?,? extends V>> |
hasValue(Matcher<? super V> valueMatcher)
Creates a matcher for
Map s matching when the examined Map contains
at least one value that satisfies the specified valueMatcher. |
static <V> Matcher<java.util.Map<?,? extends V>> |
hasValue(V value)
Creates a matcher for
Map s matching when the examined Map contains
at least one value that is equal to the specified value. |
static Matcher<org.w3c.dom.Node> |
hasXPath(java.lang.String xPath)
Creates a matcher of
Node s that matches when the examined node contains a node
at the specified xPath , with any content. |
static Matcher<org.w3c.dom.Node> |
hasXPath(java.lang.String xPath,
Matcher<java.lang.String> valueMatcher)
Creates a matcher of
Node s that matches when the examined node has a value at the
specified xPath that satisfies the specified valueMatcher . |
static Matcher<org.w3c.dom.Node> |
hasXPath(java.lang.String xPath,
javax.xml.namespace.NamespaceContext namespaceContext)
Creates a matcher of
Node s that matches when the examined node contains a node
at the specified xPath within the specified namespace context, with any content. |
static Matcher<org.w3c.dom.Node> |
hasXPath(java.lang.String xPath,
javax.xml.namespace.NamespaceContext namespaceContext,
Matcher<java.lang.String> valueMatcher)
Creates a matcher of
Node s that matches when the examined node has a value at the
specified xPath , within the specified namespaceContext , that satisfies
the specified valueMatcher . |
static <T> Matcher<T> |
in(java.util.Collection<T> collection)
Creates a matcher that matches when the examined object is found within the
specified collection.
|
static <T> Matcher<T> |
in(T[] elements)
Creates a matcher that matches when the examined object is found within the
specified array.
|
static <T> Matcher<T> |
instanceOf(java.lang.Class<?> type)
Creates a matcher that matches when the examined object is an instance of the specified
type ,
as determined by calling the Class.isInstance(Object) method on that type, passing the
the examined object. |
static <T> Matcher<T> |
is(Matcher<T> matcher)
Decorates another Matcher, retaining its behaviour, but allowing tests
to be slightly more expressive.
|
static <T> Matcher<T> |
is(T value)
A shortcut to the frequently used
is(equalTo(x)) . |
static <T> Matcher<T> |
isA(java.lang.Class<?> type)
A shortcut to the frequently used
is(instanceOf(SomeClass.class)) . |
static Matcher<java.lang.String> |
isEmptyOrNullString()
Deprecated.
use is(emptyOrNullString()) instead
|
static Matcher<java.lang.String> |
isEmptyString()
Deprecated.
use is(emptyString()) instead
|
static <T> Matcher<T> |
isIn(java.util.Collection<T> collection)
Deprecated.
use is(in(...)) instead
|
static <T> Matcher<T> |
isIn(T[] elements)
Deprecated.
use is(in(...)) instead
|
static <T> Matcher<T> |
isOneOf(T... elements)
Deprecated.
use is(oneOf(...)) instead
|
static <E> Matcher<java.lang.Iterable<E>> |
iterableWithSize(int size)
Creates a matcher for
Iterable s that matches when a single pass over the
examined Iterable yields an item count that is equal to the specified
size argument. |
static <E> Matcher<java.lang.Iterable<E>> |
iterableWithSize(Matcher<? super java.lang.Integer> sizeMatcher)
Creates a matcher for
Iterable s that matches when a single pass over the
examined Iterable yields an item count that satisfies the specified
matcher. |
static <T extends java.lang.Comparable<T>> |
lessThan(T value)
Creates a matcher of
Comparable object that matches when the examined object is
less than the specified value, as reported by the compareTo method of the
examined object. |
static <T extends java.lang.Comparable<T>> |
lessThanOrEqualTo(T value)
Creates a matcher of
Comparable object that matches when the examined object is
less than or equal to the specified value, as reported by the compareTo method
of the examined object. |
static Matcher<java.lang.String> |
matchesPattern(java.util.regex.Pattern pattern)
Creates a matcher of
String that matches when the examined string
exactly matches the given Pattern . |
static Matcher<java.lang.String> |
matchesPattern(java.lang.String regex)
Creates a matcher of
String that matches when the examined string
exactly matches the given regular expression, treated as a Pattern . |
static Matcher<java.lang.String> |
matchesRegex(java.util.regex.Pattern pattern)
Validate a string with a
Pattern . |
static Matcher<java.lang.String> |
matchesRegex(java.lang.String regex)
Validate a string with a regex.
|
static <T> Matcher<T> |
not(Matcher<T> matcher)
Creates a matcher that wraps an existing matcher, but inverts the logic by which
it will match.
|
static <T> Matcher<T> |
not(T value)
A shortcut to the frequently used
not(equalTo(x)) . |
static Matcher<java.lang.Double> |
notANumber()
Creates a matcher of
Double s that matches when an examined double is not a number. |
static Matcher<java.lang.Object> |
notNullValue()
A shortcut to the frequently used
not(nullValue()) . |
static <T> Matcher<T> |
notNullValue(java.lang.Class<T> type)
A shortcut to the frequently used
not(nullValue(X.class)). |
static Matcher<java.lang.Object> |
nullValue()
Creates a matcher that matches if examined object is
null . |
static <T> Matcher<T> |
nullValue(java.lang.Class<T> type)
Creates a matcher that matches if examined object is
null . |
static <T> Matcher<T> |
oneOf(T... elements)
Creates a matcher that matches when the examined object is equal to one of the
specified elements.
|
static <T> Matcher<T> |
sameInstance(T target)
Creates a matcher that matches only when the examined object is the same instance as
the specified target object.
|
static <B> Matcher<B> |
samePropertyValuesAs(B expectedBean,
java.lang.String... ignoredProperties)
Creates a matcher that matches when the examined object has values for all of
its JavaBean properties that are equal to the corresponding values of the
specified bean.
|
static Matcher<java.lang.String> |
startsWith(java.lang.String prefix)
Creates a matcher that matches if the examined
String starts with the specified
String . |
static Matcher<java.lang.String> |
startsWithIgnoringCase(java.lang.String prefix)
Creates a matcher that matches if the examined
String starts with the specified
String , ignoring case |
static Matcher<java.lang.String> |
stringContainsInOrder(java.lang.Iterable<java.lang.String> substrings)
Creates a matcher of
String that matches when the examined string contains all of
the specified substrings, considering the order of their appearance. |
static Matcher<java.lang.String> |
stringContainsInOrder(java.lang.String... substrings)
Creates a matcher of
String that matches when the examined string contains all of
the specified substrings, considering the order of their appearance. |
static <T> Matcher<T> |
theInstance(T target)
Creates a matcher that matches only when the examined object is the same instance as
the specified target object.
|
static <T> Matcher<java.lang.Class<?>> |
typeCompatibleWith(java.lang.Class<T> baseType)
Creates a matcher of
Class that matches when the specified baseType is
assignable from the examined class. |
public static <T> Matcher<T> allOf(java.lang.Iterable<Matcher<? super T>> matchers)
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
@SafeVarargs public static <T> Matcher<T> allOf(Matcher<? super T>... matchers)
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second)
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third)
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth)
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth)
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
public static <T> Matcher<T> allOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth, Matcher<? super T> sixth)
assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
public static <T> AnyOf<T> anyOf(java.lang.Iterable<Matcher<? super T>> matchers)
assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
@SafeVarargs public static <T> AnyOf<T> anyOf(Matcher<? super T>... matchers)
assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
public static <T> AnyOf<T> anyOf(Matcher<? super T> first, Matcher<? super T> second)
assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
public static <T> AnyOf<T> anyOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third)
assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
public static <T> AnyOf<T> anyOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth)
assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
public static <T> AnyOf<T> anyOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth)
assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
public static <T> AnyOf<T> anyOf(Matcher<? super T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth, Matcher<? super T> sixth)
assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
public static <LHS> CombinableMatcher.CombinableBothMatcher<LHS> both(Matcher<? super LHS> matcher)
assertThat("fab", both(containsString("a")).and(containsString("b")))
public static <LHS> CombinableMatcher.CombinableEitherMatcher<LHS> either(Matcher<? super LHS> matcher)
assertThat("fan", either(containsString("a")).or(containsString("b")))
public static <T> Matcher<T> describedAs(java.lang.String description, Matcher<T> matcher, java.lang.Object... values)
describedAs("a big decimal equal to %0", equalTo(myBigDecimal), myBigDecimal.toPlainString())
description
- the new description for the wrapped matchermatcher
- the matcher to wrapvalues
- optional values to insert into the tokenized descriptionpublic static <U> Matcher<java.lang.Iterable<? extends U>> everyItem(Matcher<U> itemMatcher)
Iterable
s that only matches when a single pass over the
examined Iterable
yields items that are all matched by the specified
itemMatcher
.
For example:
assertThat(Arrays.asList("bar", "baz"), everyItem(startsWith("ba")))
itemMatcher
- the matcher to apply to every item provided by the examined Iterable
public static <T> Matcher<T> is(Matcher<T> matcher)
assertThat(cheese, is(equalTo(smelly)))instead of:
assertThat(cheese, equalTo(smelly))
public static <T> Matcher<T> is(T value)
is(equalTo(x))
.
For example:
assertThat(cheese, is(smelly))instead of:
assertThat(cheese, is(equalTo(smelly)))
public static <T> Matcher<T> isA(java.lang.Class<?> type)
is(instanceOf(SomeClass.class))
.
For example:
assertThat(cheese, isA(Cheddar.class))instead of:
assertThat(cheese, is(instanceOf(Cheddar.class)))
public static Matcher<java.lang.Object> anything()
public static Matcher<java.lang.Object> anything(java.lang.String description)
String
.description
- a meaningful String
used when describing itselfpublic static <T> Matcher<java.lang.Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher)
Iterable
s that only matches when a single pass over the
examined Iterable
yields at least one item that is matched by the specified
itemMatcher
. Whilst matching, the traversal of the examined Iterable
will stop as soon as a matching item is found.
For example:
assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))
itemMatcher
- the matcher to apply to items provided by the examined Iterable
public static <T> Matcher<java.lang.Iterable<? super T>> hasItem(T item)
Iterable
s that only matches when a single pass over the
examined Iterable
yields at least one item that is equal to the specified
item
. Whilst matching, the traversal of the examined Iterable
will stop as soon as a matching item is found.
For example:
assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))
item
- the item to compare against the items provided by the examined Iterable
@SafeVarargs public static <T> Matcher<java.lang.Iterable<T>> hasItems(Matcher<? super T>... itemMatchers)
Iterable
s that matches when consecutive passes over the
examined Iterable
yield at least one item that is matched by the corresponding
matcher from the specified itemMatchers
. Whilst matching, each traversal of
the examined Iterable
will stop as soon as a matching item is found.
For example:
assertThat(Arrays.asList("foo", "bar", "baz"), hasItems(endsWith("z"), endsWith("o")))
itemMatchers
- the matchers to apply to items provided by the examined Iterable
@SafeVarargs public static <T> Matcher<java.lang.Iterable<T>> hasItems(T... items)
Iterable
s that matches when consecutive passes over the
examined Iterable
yield at least one item that is equal to the corresponding
item from the specified items
. Whilst matching, each traversal of the
examined Iterable
will stop as soon as a matching item is found.
For example:
assertThat(Arrays.asList("foo", "bar", "baz"), hasItems("baz", "foo"))
items
- the items to compare against the items provided by the examined Iterable
public static <T> Matcher<T> equalTo(T operand)
operand
, as determined by calling the Object.equals(java.lang.Object)
method on
the examined object.
If the specified operand is null
then the created matcher will only match if
the examined object's equals
method returns true
when passed a
null
(which would be a violation of the equals
contract), unless the
examined object itself is null
, in which case the matcher will return a positive
match.
The created matcher provides a special behaviour when examining Array
s, whereby
it will match if both the operand and the examined object are arrays of the same length and
contain items that are equal to each other (according to the above rules) in the same
indexes.
assertThat("foo", equalTo("foo")); assertThat(new String[] {"foo", "bar"}, equalTo(new String[] {"foo", "bar"}));
public static Matcher<java.lang.Object> equalToObject(java.lang.Object operand)
IsEqual
matcher that does not enforce the values being
compared to be of the same static type.public static <T> Matcher<T> any(java.lang.Class<T> type)
type
,
as determined by calling the Class.isInstance(Object)
method on that type, passing the
the examined object.
The created matcher forces a relationship between specified type and the examined object, and should be
used when it is necessary to make generics conform, for example in the JMock clause
with(any(Thing.class))
assertThat(new Canoe(), instanceOf(Canoe.class));
public static <T> Matcher<T> instanceOf(java.lang.Class<?> type)
type
,
as determined by calling the Class.isInstance(Object)
method on that type, passing the
the examined object.
The created matcher assumes no relationship between specified type and the examined object.
For example:assertThat(new Canoe(), instanceOf(Paddlable.class));
public static <T> Matcher<T> not(Matcher<T> matcher)
assertThat(cheese, is(not(equalTo(smelly))))
matcher
- the matcher whose sense should be invertedpublic static <T> Matcher<T> not(T value)
not(equalTo(x))
.
For example:
assertThat(cheese, is(not(smelly)))instead of:
assertThat(cheese, is(not(equalTo(smelly))))
value
- the value that any examined object should not equalpublic static Matcher<java.lang.Object> notNullValue()
not(nullValue())
.
For example:
assertThat(cheese, is(notNullValue()))instead of:
assertThat(cheese, is(not(nullValue())))
public static <T> Matcher<T> notNullValue(java.lang.Class<T> type)
not(nullValue(X.class)). Accepts a
single dummy argument to facilitate type inference.
.
For example:
assertThat(cheese, is(notNullValue(X.class)))instead of:
assertThat(cheese, is(not(nullValue(X.class))))
type
- dummy parameter used to infer the generic type of the returned matcherpublic static Matcher<java.lang.Object> nullValue()
null
.
For example:
assertThat(cheese, is(nullValue())
public static <T> Matcher<T> nullValue(java.lang.Class<T> type)
null
. Accepts a
single dummy argument to facilitate type inference.
For example:
assertThat(cheese, is(nullValue(Cheese.class))
type
- dummy parameter used to infer the generic type of the returned matcherpublic static <T> Matcher<T> sameInstance(T target)
target
- the target instance against which others should be assessedpublic static <T> Matcher<T> theInstance(T target)
target
- the target instance against which others should be assessedpublic static Matcher<java.lang.String> containsString(java.lang.String substring)
String
contains the specified
String
anywhere.
For example:
assertThat("myStringOfNote", containsString("ring"))
substring
- the substring that the returned matcher will expect to find within any examined stringpublic static Matcher<java.lang.String> containsStringIgnoringCase(java.lang.String substring)
String
contains the specified
String
anywhere, ignoring case.
For example:
assertThat("myStringOfNote", containsStringIgnoringCase("Ring"))
substring
- the substring that the returned matcher will expect to find within any examined stringpublic static Matcher<java.lang.String> startsWith(java.lang.String prefix)
Creates a matcher that matches if the examined String
starts with the specified
String
.
assertThat("myStringOfNote", startsWith("my"))
prefix
- the substring that the returned matcher will expect at the start of any examined stringpublic static Matcher<java.lang.String> startsWithIgnoringCase(java.lang.String prefix)
Creates a matcher that matches if the examined String
starts with the specified
String
, ignoring case
assertThat("myStringOfNote", startsWithIgnoringCase("My"))
prefix
- the substring that the returned matcher will expect at the start of any examined stringpublic static Matcher<java.lang.String> endsWith(java.lang.String suffix)
String
ends with the specified
String
.
For example:
assertThat("myStringOfNote", endsWith("Note"))
suffix
- the substring that the returned matcher will expect at the end of any examined stringpublic static Matcher<java.lang.String> endsWithIgnoringCase(java.lang.String suffix)
String
ends with the specified
String
, ignoring case.
For example:
assertThat("myStringOfNote", endsWithIgnoringCase("note"))
suffix
- the substring that the returned matcher will expect at the end of any examined stringpublic static Matcher<java.lang.String> matchesRegex(java.util.regex.Pattern pattern)
Pattern
.
assertThat("abc", matchesRegex(Pattern.compile("ˆ[a-z]$"));
pattern
- the pattern to be used.public static Matcher<java.lang.String> matchesRegex(java.lang.String regex)
assertThat("abc", matchesRegex("ˆ[a-z]+$"));
regex
- The regex to be used for the validation.@SafeVarargs public static <T> IsArray<T> array(Matcher<? super T>... elementMatchers)
assertThat(new Integer[]{1,2,3}, is(array(equalTo(1), equalTo(2), equalTo(3))))
elementMatchers
- the matchers that the elements of examined arrays should satisfypublic static <T> Matcher<T[]> hasItemInArray(Matcher<? super T> elementMatcher)
elementMatcher
. Whilst matching, the traversal
of the examined array will stop as soon as a matching element is found.
For example:
assertThat(new String[] {"foo", "bar"}, hasItemInArray(startsWith("ba")))
elementMatcher
- the matcher to apply to elements in examined arrayspublic static <T> Matcher<T[]> hasItemInArray(T element)
hasItemInArray(equalTo(x))
.
For example:
assertThat(hasItemInArray(x))instead of:
assertThat(hasItemInArray(equalTo(x)))
element
- the element that should be present in examined arrays@SafeVarargs public static <E> Matcher<E[]> arrayContaining(E... items)
assertThat(new String[]{"foo", "bar"}, arrayContaining("foo", "bar"))
items
- the items that must equal the items within an examined array@SafeVarargs public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatchers)
assertThat(new String[]{"foo", "bar"}, arrayContaining(equalTo("foo"), equalTo("bar")))
itemMatchers
- the matchers that must be satisfied by the items in the examined arraypublic static <E> Matcher<E[]> arrayContaining(java.util.List<Matcher<? super E>> itemMatchers)
assertThat(new String[]{"foo", "bar"}, arrayContaining(Arrays.asList(equalTo("foo"), equalTo("bar"))))
itemMatchers
- a list of matchers, each of which must be satisfied by the corresponding item in an examined array@SafeVarargs public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<? super E>... itemMatchers)
Creates an order agnostic matcher for arrays that matches when each item in the examined array satisfies one matcher anywhere in the specified matchers. For a positive match, the examined array must be of the same length as the number of specified matchers.
N.B. each of the specified matchers will only be used once during a given examination, so be careful when specifying matchers that may be satisfied by more than one entry in an examined array.
For example:
assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(equalTo("bar"), equalTo("foo")))
itemMatchers
- a list of matchers, each of which must be satisfied by an entry in an examined arraypublic static <E> Matcher<E[]> arrayContainingInAnyOrder(java.util.Collection<Matcher<? super E>> itemMatchers)
Creates an order agnostic matcher for arrays that matches when each item in the examined array satisfies one matcher anywhere in the specified collection of matchers. For a positive match, the examined array must be of the same length as the specified collection of matchers.
N.B. each matcher in the specified collection will only be used once during a given examination, so be careful when specifying matchers that may be satisfied by more than one entry in an examined array.
For example:
assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))
itemMatchers
- a list of matchers, each of which must be satisfied by an item provided by an examined array@SafeVarargs public static <E> Matcher<E[]> arrayContainingInAnyOrder(E... items)
Creates an order agnostic matcher for arrays that matches when each item in the examined array is logically equal to one item anywhere in the specified items. For a positive match, the examined array must be of the same length as the number of specified items.
N.B. each of the specified items will only be used once during a given examination, so be careful when specifying items that may be equal to more than one entry in an examined array.
For example:
assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder("bar", "foo"))
items
- the items that must equal the entries of an examined array, in any orderpublic static <E> Matcher<E[]> arrayWithSize(Matcher<? super java.lang.Integer> sizeMatcher)
length
of the array
satisfies the specified matcher.
For example:
assertThat(new String[]{"foo", "bar"}, arrayWithSize(equalTo(2)))
sizeMatcher
- a matcher for the length of an examined arraypublic static <E> Matcher<E[]> arrayWithSize(int size)
length
of the array
equals the specified size
.
For example:
assertThat(new String[]{"foo", "bar"}, arrayWithSize(2))
size
- the length that an examined array must have for a positive matchpublic static <E> Matcher<E[]> emptyArray()
length
of the array
is zero.
For example:
assertThat(new String[0], emptyArray())
public static <K,V> Matcher<java.util.Map<? extends K,? extends V>> aMapWithSize(Matcher<? super java.lang.Integer> sizeMatcher)
Map
s that matches when the size()
method returns
a value that satisfies the specified matcher.
For example:
assertThat(myMap, is(aMapWithSize(equalTo(2))))
sizeMatcher
- a matcher for the size of an examined Map
public static <K,V> Matcher<java.util.Map<? extends K,? extends V>> aMapWithSize(int size)
Map
s that matches when the size()
method returns
a value equal to the specified size
.
For example:
assertThat(myMap, is(aMapWithSize(2)))
size
- the expected size of an examined Map
public static <K,V> Matcher<java.util.Map<? extends K,? extends V>> anEmptyMap()
Map
s that matches when the size()
method returns
zero.
For example:
assertThat(myMap, is(anEmptyMap()))
public static <E> Matcher<java.util.Collection<? extends E>> hasSize(Matcher<? super java.lang.Integer> sizeMatcher)
Collection
s that matches when the size()
method returns
a value that satisfies the specified matcher.
For example:
assertThat(Arrays.asList("foo", "bar"), hasSize(equalTo(2)))
sizeMatcher
- a matcher for the size of an examined Collection
public static <E> Matcher<java.util.Collection<? extends E>> hasSize(int size)
Collection
s that matches when the size()
method returns
a value equal to the specified size
.
For example:
assertThat(Arrays.asList("foo", "bar"), hasSize(2))
size
- the expected size of an examined Collection
public static <E> Matcher<java.util.Collection<? extends E>> empty()
Collection
s matching examined collections whose isEmpty
method returns true
.
For example:
assertThat(new ArrayList<String>(), is(empty()))
public static <E> Matcher<java.util.Collection<E>> emptyCollectionOf(java.lang.Class<E> unusedToForceReturnType)
Collection
s matching examined collections whose isEmpty
method returns true
.
For example:
assertThat(new ArrayList<String>(), is(emptyCollectionOf(String.class)))
unusedToForceReturnType
- the type of the collection's contentpublic static <E> Matcher<java.lang.Iterable<? extends E>> emptyIterable()
Iterable
s matching examined iterables that yield no items.
For example:
assertThat(new ArrayList<String>(), is(emptyIterable()))
public static <E> Matcher<java.lang.Iterable<E>> emptyIterableOf(java.lang.Class<E> unusedToForceReturnType)
Iterable
s matching examined iterables that yield no items.
For example:
assertThat(new ArrayList<String>(), is(emptyIterableOf(String.class)))
unusedToForceReturnType
- the type of the iterable's content@SafeVarargs public static <E> Matcher<java.lang.Iterable<? extends E>> contains(E... items)
Iterable
s that matches when a single pass over the
examined Iterable
yields a series of items, each logically equal to the
corresponding item in the specified items. For a positive match, the examined iterable
must be of the same length as the number of specified items.
For example:
assertThat(Arrays.asList("foo", "bar"), contains("foo", "bar"))
items
- the items that must equal the items provided by an examined Iterable
public static <E> Matcher<java.lang.Iterable<? extends E>> contains(Matcher<? super E> itemMatcher)
Iterable
s that matches when a single pass over the
examined Iterable
yields a single item that satisfies the specified matcher.
For a positive match, the examined iterable must only yield one item.
For example:
assertThat(Arrays.asList("foo"), contains(equalTo("foo")))
itemMatcher
- the matcher that must be satisfied by the single item provided by an
examined Iterable
@SafeVarargs public static <E> Matcher<java.lang.Iterable<? extends E>> contains(Matcher<? super E>... itemMatchers)
Iterable
s that matches when a single pass over the
examined Iterable
yields a series of items, each satisfying the corresponding
matcher in the specified matchers. For a positive match, the examined iterable
must be of the same length as the number of specified matchers.
For example:
assertThat(Arrays.asList("foo", "bar"), contains(equalTo("foo"), equalTo("bar")))
itemMatchers
- the matchers that must be satisfied by the items provided by an examined Iterable
public static <E> Matcher<java.lang.Iterable<? extends E>> contains(java.util.List<Matcher<? super E>> itemMatchers)
Iterable
s that matches when a single pass over the
examined Iterable
yields a series of items, each satisfying the corresponding
matcher in the specified list of matchers. For a positive match, the examined iterable
must be of the same length as the specified list of matchers.
For example:
assertThat(Arrays.asList("foo", "bar"), contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))
itemMatchers
- a list of matchers, each of which must be satisfied by the corresponding item provided by
an examined Iterable
@SafeVarargs public static <T> Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(Matcher<? super T>... itemMatchers)
Creates an order agnostic matcher for Iterable
s that matches when a single pass over
the examined Iterable
yields a series of items, each satisfying one matcher anywhere
in the specified matchers. For a positive match, the examined iterable must be of the same
length as the number of specified matchers.
N.B. each of the specified matchers will only be used once during a given examination, so be careful when specifying matchers that may be satisfied by more than one entry in an examined iterable.
For example:
assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(equalTo("bar"), equalTo("foo")))
itemMatchers
- a list of matchers, each of which must be satisfied by an item provided by an examined Iterable
@SafeVarargs public static <T> Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(T... items)
Creates an order agnostic matcher for Iterable
s that matches when a single pass over
the examined Iterable
yields a series of items, each logically equal to one item
anywhere in the specified items. For a positive match, the examined iterable
must be of the same length as the number of specified items.
N.B. each of the specified items will only be used once during a given examination, so be careful when specifying items that may be equal to more than one entry in an examined iterable.
For example:
assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder("bar", "foo"))
items
- the items that must equal the items provided by an examined Iterable
in any orderpublic static <T> Matcher<java.lang.Iterable<? extends T>> containsInAnyOrder(java.util.Collection<Matcher<? super T>> itemMatchers)
Creates an order agnostic matcher for Iterable
s that matches when a single pass over
the examined Iterable
yields a series of items, each satisfying one matcher anywhere
in the specified collection of matchers. For a positive match, the examined iterable
must be of the same length as the specified collection of matchers.
N.B. each matcher in the specified collection will only be used once during a given examination, so be careful when specifying matchers that may be satisfied by more than one entry in an examined iterable.
For example:
assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))
itemMatchers
- a list of matchers, each of which must be satisfied by an item provided by an examined Iterable
@SafeVarargs public static <E> Matcher<java.lang.Iterable<? extends E>> containsInRelativeOrder(E... items)
Iterable
s that matches when a single pass over the
examined Iterable
yields a series of items, that contains items logically equal to the
corresponding item in the specified items, in the same relative order
For example:
assertThat(Arrays.asList("a", "b", "c", "d", "e"), containsInRelativeOrder("b", "d"))
items
- the items that must be contained within items provided by an examined Iterable
in the same relative order@SafeVarargs public static <E> Matcher<java.lang.Iterable<? extends E>> containsInRelativeOrder(Matcher<? super E>... itemMatchers)
Iterable
s that matches when a single pass over the
examined Iterable
yields a series of items, that each satisfying the corresponding
matcher in the specified matchers, in the same relative order.
For example:
assertThat(Arrays.asList("a", "b", "c", "d", "e"), containsInRelativeOrder(equalTo("b"), equalTo("d")))
itemMatchers
- the matchers that must be satisfied by the items provided by an examined Iterable
in the same relative orderpublic static <E> Matcher<java.lang.Iterable<? extends E>> containsInRelativeOrder(java.util.List<Matcher<? super E>> itemMatchers)
Iterable
s that matches when a single pass over the
examined Iterable
yields a series of items, that contains items satisfying the corresponding
matcher in the specified list of matchers, in the same relative order.
For example:
assertThat(Arrays.asList("a", "b", "c", "d", "e"), contains(Arrays.asList(equalTo("b"), equalTo("d"))))
itemMatchers
- a list of matchers, each of which must be satisfied by the items provided by
an examined Iterable
in the same relative orderpublic static <E> Matcher<java.lang.Iterable<E>> iterableWithSize(Matcher<? super java.lang.Integer> sizeMatcher)
Iterable
s that matches when a single pass over the
examined Iterable
yields an item count that satisfies the specified
matcher.
For example:
assertThat(Arrays.asList("foo", "bar"), iterableWithSize(equalTo(2)))
sizeMatcher
- a matcher for the number of items that should be yielded by an examined Iterable
public static <E> Matcher<java.lang.Iterable<E>> iterableWithSize(int size)
Iterable
s that matches when a single pass over the
examined Iterable
yields an item count that is equal to the specified
size
argument.
For example:
assertThat(Arrays.asList("foo", "bar"), iterableWithSize(2))
size
- the number of items that should be yielded by an examined Iterable
public static <K,V> Matcher<java.util.Map<? extends K,? extends V>> hasEntry(Matcher<? super K> keyMatcher, Matcher<? super V> valueMatcher)
Map
s matching when the examined Map
contains
at least one entry whose key satisfies the specified keyMatcher
and whose
value satisfies the specified valueMatcher
.
For example:
assertThat(myMap, hasEntry(equalTo("bar"), equalTo("foo")))
keyMatcher
- the key matcher that, in combination with the valueMatcher, must be satisfied by at least one entryvalueMatcher
- the value matcher that, in combination with the keyMatcher, must be satisfied by at least one entrypublic static <K,V> Matcher<java.util.Map<? extends K,? extends V>> hasEntry(K key, V value)
Map
s matching when the examined Map
contains
at least one entry whose key equals the specified key
and whose value equals the
specified value
.
For example:
assertThat(myMap, hasEntry("bar", "foo"))
key
- the key that, in combination with the value, must be describe at least one entryvalue
- the value that, in combination with the key, must be describe at least one entrypublic static <K> Matcher<java.util.Map<? extends K,?>> hasKey(Matcher<? super K> keyMatcher)
Map
s matching when the examined Map
contains
at least one key that satisfies the specified matcher.
For example:
assertThat(myMap, hasKey(equalTo("bar")))
keyMatcher
- the matcher that must be satisfied by at least one keypublic static <K> Matcher<java.util.Map<? extends K,?>> hasKey(K key)
Map
s matching when the examined Map
contains
at least one key that is equal to the specified key.
For example:
assertThat(myMap, hasKey("bar"))
key
- the key that satisfying maps must containpublic static <V> Matcher<java.util.Map<?,? extends V>> hasValue(Matcher<? super V> valueMatcher)
Map
s matching when the examined Map
contains
at least one value that satisfies the specified valueMatcher.
For example:
assertThat(myMap, hasValue(equalTo("foo")))
valueMatcher
- the matcher that must be satisfied by at least one valuepublic static <V> Matcher<java.util.Map<?,? extends V>> hasValue(V value)
Map
s matching when the examined Map
contains
at least one value that is equal to the specified value.
For example:
assertThat(myMap, hasValue("foo"))
value
- the value that satisfying maps must containpublic static <T> Matcher<T> in(java.util.Collection<T> collection)
assertThat("foo", is(in(Arrays.asList("bar", "foo"))))
collection
- the collection in which matching items must be foundpublic static <T> Matcher<T> in(T[] elements)
assertThat("foo", is(in(new String[]{"bar", "foo"})))
elements
- the array in which matching items must be foundpublic static <T> Matcher<T> isIn(java.util.Collection<T> collection)
assertThat("foo", isIn(Arrays.asList("bar", "foo")))
collection
- the collection in which matching items must be foundpublic static <T> Matcher<T> isIn(T[] elements)
assertThat("foo", isIn(new String[]{"bar", "foo"}))
elements
- the array in which matching items must be found@SafeVarargs public static <T> Matcher<T> isOneOf(T... elements)
assertThat("foo", isOneOf("bar", "foo"))
elements
- the elements amongst which matching items will be found@SafeVarargs public static <T> Matcher<T> oneOf(T... elements)
assertThat("foo", is(oneOf("bar", "foo")))
elements
- the elements amongst which matching items will be foundpublic static Matcher<java.lang.Double> closeTo(double operand, double error)
Double
s that matches when an examined double is equal
to the specified operand
, within a range of +/- error
.
For example:
assertThat(1.03, is(closeTo(1.0, 0.03)))
operand
- the expected value of matching doubleserror
- the delta (+/-) within which matches will be allowedpublic static Matcher<java.lang.Double> notANumber()
Double
s that matches when an examined double is not a number.
For example:
assertThat(Double.NaN, is(notANumber()))
public static Matcher<java.math.BigDecimal> closeTo(java.math.BigDecimal operand, java.math.BigDecimal error)
BigDecimal
s that matches when an examined BigDecimal is equal
to the specified operand
, within a range of +/- error
. The comparison for equality
is done by BigDecimals BigDecimal.compareTo(java.math.BigDecimal)
method.
For example:
assertThat(new BigDecimal("1.03"), is(closeTo(new BigDecimal("1.0"), new BigDecimal("0.03"))))
operand
- the expected value of matching BigDecimalserror
- the delta (+/-) within which matches will be allowedpublic static <T extends java.lang.Comparable<T>> Matcher<T> comparesEqualTo(T value)
Comparable
object that matches when the examined object is
equal to the specified value, as reported by the compareTo
method of the
examined object.
For example:
assertThat(1, comparesEqualTo(1))
value
- the value which, when passed to the compareTo method of the examined object, should return zeropublic static <T extends java.lang.Comparable<T>> Matcher<T> greaterThan(T value)
Comparable
object that matches when the examined object is
greater than the specified value, as reported by the compareTo
method of the
examined object.
For example:
assertThat(2, greaterThan(1))
value
- the value which, when passed to the compareTo method of the examined object, should return greater
than zeropublic static <T extends java.lang.Comparable<T>> Matcher<T> greaterThanOrEqualTo(T value)
Comparable
object that matches when the examined object is
greater than or equal to the specified value, as reported by the compareTo
method
of the examined object.
For example:
assertThat(1, greaterThanOrEqualTo(1))
value
- the value which, when passed to the compareTo method of the examined object, should return greater
than or equal to zeropublic static <T extends java.lang.Comparable<T>> Matcher<T> lessThan(T value)
Comparable
object that matches when the examined object is
less than the specified value, as reported by the compareTo
method of the
examined object.
For example:
assertThat(1, lessThan(2))
value
- the value which, when passed to the compareTo method of the examined object, should return less
than zeropublic static <T extends java.lang.Comparable<T>> Matcher<T> lessThanOrEqualTo(T value)
Comparable
object that matches when the examined object is
less than or equal to the specified value, as reported by the compareTo
method
of the examined object.
For example:
assertThat(1, lessThanOrEqualTo(1))
value
- the value which, when passed to the compareTo method of the examined object, should return less
than or equal to zeropublic static Matcher<java.lang.String> equalToIgnoringCase(java.lang.String expectedString)
String
that matches when the examined string is equal to
the specified expectedString, ignoring case.
For example:
assertThat("Foo", equalToIgnoringCase("FOO"))
expectedString
- the expected value of matched stringspublic static Matcher<java.lang.String> equalToIgnoringWhiteSpace(java.lang.String expectedString)
equalToCompressingWhiteSpace(String)
expectedString
- the expected value of matched stringspublic static Matcher<java.lang.String> equalToCompressingWhiteSpace(java.lang.String expectedString)
String
that matches when the examined string is equal to
the specified expectedString, when whitespace differences are (mostly) ignored. To be
exact, the following whitespace rules are applied:
assertThat(" my\tfoo bar ", equalToIgnoringWhiteSpace(" my foo bar"))
expectedString
- the expected value of matched stringspublic static Matcher<java.lang.String> emptyOrNullString()
String
that matches when the examined string is null
, or
has zero length.
For example:
assertThat(((String)null), is(emptyOrNullString()))
public static Matcher<java.lang.String> emptyString()
String
that matches when the examined string has zero length.
For example:
assertThat("", is(emptyString()))
public static Matcher<java.lang.String> isEmptyOrNullString()
String
that matches when the examined string is null
, or
has zero length.
For example:
assertThat(((String)null), isEmptyOrNullString())
public static Matcher<java.lang.String> isEmptyString()
String
that matches when the examined string has zero length.
For example:
assertThat("", isEmptyString())
public static Matcher<java.lang.String> blankOrNullString()
String
that matches when the examined string is null
, or
contains zero or more whitespace characters and nothing else.
For example:
assertThat(((String)null), is(blankOrNullString()))
public static Matcher<java.lang.String> blankString()
String
that matches when the examined string contains
zero or more whitespace characters and nothing else.
For example:
assertThat(" ", is(blankString()))
public static Matcher<java.lang.String> matchesPattern(java.util.regex.Pattern pattern)
String
that matches when the examined string
exactly matches the given Pattern
.public static Matcher<java.lang.String> matchesPattern(java.lang.String regex)
String
that matches when the examined string
exactly matches the given regular expression, treated as a Pattern
.public static Matcher<java.lang.String> stringContainsInOrder(java.lang.Iterable<java.lang.String> substrings)
String
that matches when the examined string contains all of
the specified substrings, considering the order of their appearance.
For example:
assertThat("myfoobarbaz", stringContainsInOrder(Arrays.asList("bar", "foo")))fails as "foo" occurs before "bar" in the string "myfoobarbaz"
substrings
- the substrings that must be contained within matching stringspublic static Matcher<java.lang.String> stringContainsInOrder(java.lang.String... substrings)
String
that matches when the examined string contains all of
the specified substrings, considering the order of their appearance.
For example:
assertThat("myfoobarbaz", stringContainsInOrder("bar", "foo"))fails as "foo" occurs before "bar" in the string "myfoobarbaz"
substrings
- the substrings that must be contained within matching stringspublic static Matcher<java.lang.CharSequence> hasLength(int length)
CharSequence
that matches when a char sequence has the length
of the specified argument
.
For example:
assertThat("text", length(4))
length
- the expected length of the stringpublic static <T> Matcher<T> hasToString(Matcher<? super java.lang.String> toStringMatcher)
toString
method
returns a value that satisfies the specified matcher.
For example:
assertThat(true, hasToString(equalTo("TRUE")))
toStringMatcher
- the matcher used to verify the toString resultpublic static <T> Matcher<T> hasToString(java.lang.String expectedToString)
toString
method
returns a value equalTo the specified string.
For example:
assertThat(true, hasToString("TRUE"))
expectedToString
- the expected toString resultpublic static <T> Matcher<java.lang.Class<?>> typeCompatibleWith(java.lang.Class<T> baseType)
Class
that matches when the specified baseType is
assignable from the examined class.
For example:
assertThat(Integer.class, typeCompatibleWith(Number.class))
baseType
- the base class to examine classes againstpublic static Matcher<java.util.EventObject> eventFrom(java.lang.Class<? extends java.util.EventObject> eventClass, java.lang.Object source)
EventObject
that matches any object
derived from eventClass announced by source.
For example:
assertThat(myEvent, is(eventFrom(PropertyChangeEvent.class, myBean)))
eventClass
- the class of the event to match onsource
- the source of the eventpublic static Matcher<java.util.EventObject> eventFrom(java.lang.Object source)
EventObject
that matches any EventObject
announced by source.
For example:
assertThat(myEvent, is(eventFrom(myBean)))
source
- the source of the eventpublic static <T> Matcher<T> hasProperty(java.lang.String propertyName)
assertThat(myBean, hasProperty("foo"))
propertyName
- the name of the JavaBean property that examined beans should possesspublic static <T> Matcher<T> hasProperty(java.lang.String propertyName, Matcher<?> valueMatcher)
assertThat(myBean, hasProperty("foo", equalTo("bar"))
propertyName
- the name of the JavaBean property that examined beans should possessvalueMatcher
- a matcher for the value of the specified property of the examined beanpublic static <B> Matcher<B> samePropertyValuesAs(B expectedBean, java.lang.String... ignoredProperties)
agerather than method names such as
getAge. For example:
assertThat(myBean, samePropertyValuesAs(myExpectedBean))
assertThat(myBean, samePropertyValuesAs(myExpectedBean), "age", "height")
expectedBean
- the bean against which examined beans are comparedignoredProperties
- do not check any of these named properties.public static Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, Matcher<java.lang.String> valueMatcher)
Node
s that matches when the examined node has a value at the
specified xPath
that satisfies the specified valueMatcher
.
For example:
assertThat(xml, hasXPath("/root/something[2]/cheese", equalTo("Cheddar")))
xPath
- the target xpathvalueMatcher
- matcher for the value at the specified xpathpublic static Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, Matcher<java.lang.String> valueMatcher)
Node
s that matches when the examined node has a value at the
specified xPath
, within the specified namespaceContext
, that satisfies
the specified valueMatcher
.
For example:
assertThat(xml, hasXPath("/root/something[2]/cheese", myNs, equalTo("Cheddar")))
xPath
- the target xpathnamespaceContext
- the namespace for matching nodesvalueMatcher
- matcher for the value at the specified xpathpublic static Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath)
Node
s that matches when the examined node contains a node
at the specified xPath
, with any content.
For example:
assertThat(xml, hasXPath("/root/something[2]/cheese"))
xPath
- the target xpathpublic static Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext)
Node
s that matches when the examined node contains a node
at the specified xPath
within the specified namespace context, with any content.
For example:
assertThat(xml, hasXPath("/root/something[2]/cheese", myNs))
xPath
- the target xpathnamespaceContext
- the namespace for matching nodes