Top Constructors Methods
org.hamcrest

public Class CoreMatchers

extends Object
Class Inheritance
Annotations
@SuppressWarnings:UnusedDeclaration
Imports
org.hamcrest.core.IsIterableContaining

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static <T> Matcher<T>
allOf(Iterable<Matcher<? super T>> matchers)

Creates a matcher that matches if the examined object matches ALL of the specified matchers.

public static <T> Matcher<T>
allOf(Matcher<? super T>... matchers)

Creates a matcher that matches if the examined object matches ALL of the specified matchers.

public static <T> Matcher<T>
any(Class<T> type)

Creates a matcher that matches when the examined object is an instance of the specified type, as determined by calling the java.lang.Class#isInstance(Object) method on that type, passing the the examined object.

public static <T> AnyOf<T>
anyOf(Iterable<Matcher<? super T>> matchers)

Creates a matcher that matches if the examined object matches ANY of the specified matchers.

public static <T> AnyOf<T>
anyOf(Matcher<? super T>... matchers)

Creates a matcher that matches if the examined object matches ANY of the specified matchers.

public static Matcher<Object>
anything()

Creates a matcher that always matches, regardless of the examined object.

public static Matcher<Object>
anything(String
a meaningful String used when describing itself
description
)

Creates a matcher that always matches, regardless of the examined object, but describes itself with the specified String.

public 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.

public static Matcher<String>
containsString(String
the substring that the returned matcher will expect to find within any examined string
substring
)

Creates a matcher that matches if the examined String contains the specified String anywhere.

public static Matcher<String>
containsStringIgnoringCase(String
the substring that the returned matcher will expect to find within any examined string
substring
)

Creates a matcher that matches if the examined String contains the specified String anywhere, ignoring case.

public static <T> Matcher<T>
describedAs(String
the new description for the wrapped matcher
description
,
Matcher<T>
the matcher to wrap
matcher
,
Object...
optional values to insert into the tokenised description
values
)

Wraps an existing matcher, overriding its description with that specified.

public 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.

public static Matcher<String>
endsWith(String
the substring that the returned matcher will expect at the end of any examined string
suffix
)

Creates a matcher that matches if the examined String ends with the specified String.

public static Matcher<String>
endsWithIgnoringCase(String
the substring that the returned matcher will expect at the end of any examined string
suffix
)

Creates a matcher that matches if the examined String ends with the specified String, ignoring case.

public 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 java.lang.Object#equals method on the examined object.

public static Matcher<Object>
equalToObject(Object operand)

Creates an org.hamcrest.core.IsEqual matcher that does not enforce the values being compared to be of the same static type.

public static <U> Matcher<Iterable<? extends U>>
everyItem(Matcher<U>
the matcher to apply to every item provided by the examined Iterable
itemMatcher
)

Creates a matcher for Iterables that only matches when a single pass over the examined Iterable yields items that are all matched by the specified itemMatcher.

public static <T> Matcher<Iterable<? super T>>
hasItem(Matcher<? super T>
the matcher to apply to items provided by the examined Iterable
itemMatcher
)

Creates a matcher for Iterables that only matches when a single pass over the examined Iterable yields at least one item that is matched by the specified itemMatcher.

public static <T> Matcher<Iterable<? super T>>
hasItem(T
the item to compare against the items provided by the examined Iterable
item
)

Creates a matcher for Iterables that only matches when a single pass over the examined Iterable yields at least one item that is equal to the specified item.

public static <T> Matcher<Iterable<T>>
hasItems(Matcher<? super T>...
the matchers to apply to items provided by the examined Iterable
itemMatchers
)

Creates a matcher for Iterables 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.

public static <T> Matcher<Iterable<T>>
hasItems(T...
the items to compare against the items provided by the examined Iterable
items
)

Creates a matcher for Iterables 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.

public static <T> Matcher<T>
instanceOf(Class<?> type)

Creates a matcher that matches when the examined object is an instance of the specified type, as determined by calling the java.lang.Class#isInstance(Object) method on that type, passing the the examined object.

public static <T> Matcher<T>
is(Matcher<T> matcher)

Decorates another Matcher, retaining its behaviour, but allowing tests to be slightly more expressive.

public static <T> Matcher<T>
is(T value)

A shortcut to the frequently used is(equalTo(x)).

public static <T> Matcher<T>
isA(Class<T> type)

A shortcut to the frequently used is(instanceOf(SomeClass.class)).

public static <T> Matcher<T>
not(Matcher<T>
the matcher whose sense should be inverted
matcher
)

Creates a matcher that wraps an existing matcher, but inverts the logic by which it will match.

public static <T> Matcher<T>
not(T
the value that any examined object should not equal
value
)

A shortcut to the frequently used not(equalTo(x)).

public static Matcher<Object>
notNullValue()

A shortcut to the frequently used not(nullValue()).

public static <T> Matcher<T>
notNullValue(Class<T>
dummy parameter used to infer the generic type of the returned matcher
type
)

A shortcut to the frequently used not(nullValue(X.class)).

public static Matcher<Object>
nullValue()

Creates a matcher that matches if examined object is null.

public static <T> Matcher<T>
nullValue(Class<T>
dummy parameter used to infer the generic type of the returned matcher
type
)

Creates a matcher that matches if examined object is null.

public static <T> Matcher<T>
sameInstance(T
the target instance against which others should be assessed
target
)

Creates a matcher that matches only when the examined object is the same instance as the specified target object.

public static Matcher<String>
startsWith(String
the substring that the returned matcher will expect at the start of any examined string
prefix
)

Creates a matcher that matches if the examined String starts with the specified String.

public static Matcher<String>
startsWithIgnoringCase(String
the substring that the returned matcher will expect at the start of any examined string
prefix
)

Creates a matcher that matches if the examined String starts with the specified String, ignoring case For example:

assertThat("myStringOfNote", startsWith("my"))
public static <T> Matcher<T>
theInstance(T
the target instance against which others should be assessed
target
)

Creates a matcher that matches only when the examined object is the same instance as the specified target object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CoreMatchersback to summary
public CoreMatchers()

Method Detail

allOfback to summary
public static <T> Matcher<T> allOf(Iterable<Matcher<? super T>> matchers)

Creates a matcher that matches if the examined object matches ALL of the specified matchers. For example:

assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
allOfback to summary
public static <T> Matcher<T> allOf(Matcher<? super T>... matchers)

Creates a matcher that matches if the examined object matches ALL of the specified matchers. For example:

assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
Annotations
@SafeVarargs
anyback to summary
public static <T> Matcher<T> any(Class<T> type)

Creates a matcher that matches when the examined object is an instance of the specified type, as determined by calling the java.lang.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))

For example:
assertThat(new Canoe(), instanceOf(Canoe.class));
anyOfback to summary
public static <T> AnyOf<T> anyOf(Iterable<Matcher<? super T>> matchers)

Creates a matcher that matches if the examined object matches ANY of the specified matchers. For example:

assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
anyOfback to summary
public static <T> AnyOf<T> anyOf(Matcher<? super T>... matchers)

Creates a matcher that matches if the examined object matches ANY of the specified matchers. For example:

assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
Annotations
@SafeVarargs
anythingback to summary
public static Matcher<Object> anything()

Creates a matcher that always matches, regardless of the examined object.

anythingback to summary
public static Matcher<Object> anything(String description)

Creates a matcher that always matches, regardless of the examined object, but describes itself with the specified String.

Parameters
description:String

a meaningful String used when describing itself

bothback to summary
public 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. For example:

assertThat("fab", both(containsString("a")).and(containsString("b")))
containsStringback to summary
public static Matcher<String> containsString(String substring)

Creates a matcher that matches if the examined String contains the specified String anywhere. For example:

assertThat("myStringOfNote", containsString("ring"))
Parameters
substring:String

the substring that the returned matcher will expect to find within any examined string

containsStringIgnoringCaseback to summary
public static Matcher<String> containsStringIgnoringCase(String substring)

Creates a matcher that matches if the examined String contains the specified String anywhere, ignoring case. For example:

assertThat("myStringOfNote", containsString("ring"))
Parameters
substring:String

the substring that the returned matcher will expect to find within any examined string

describedAsback to summary
public static <T> Matcher<T> describedAs(String description, Matcher<T> matcher, Object... values)

Wraps an existing matcher, overriding its description with that specified. All other functions are delegated to the decorated matcher, including its mismatch description. For example:

describedAs("a big decimal equal to %0", equalTo(myBigDecimal), myBigDecimal.toPlainString())
Parameters
description:String

the new description for the wrapped matcher

matcher:Matcher<T>

the matcher to wrap

values:Object[]

optional values to insert into the tokenised description

eitherback to summary
public 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. For example:

assertThat("fan", either(containsString("a")).or(containsString("b")))
endsWithback to summary
public static Matcher<String> endsWith(String suffix)

Creates a matcher that matches if the examined String ends with the specified String. For example:

assertThat("myStringOfNote", endsWith("Note"))
Parameters
suffix:String

the substring that the returned matcher will expect at the end of any examined string

endsWithIgnoringCaseback to summary
public static Matcher<String> endsWithIgnoringCase(String suffix)

Creates a matcher that matches if the examined String ends with the specified String, ignoring case. For example:

assertThat("myStringOfNote", endsWith("Note"))
Parameters
suffix:String

the substring that the returned matcher will expect at the end of any examined string

equalToback to summary
public 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 java.lang.Object#equals 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 Arrays, 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.

For example:
assertThat("foo", equalTo("foo"));
assertThat(new String[] {"foo", "bar"}, equalTo(new String[] {"foo", "bar"}));
equalToObjectback to summary
public static Matcher<Object> equalToObject(Object operand)

Creates an org.hamcrest.core.IsEqual matcher that does not enforce the values being compared to be of the same static type.

everyItemback to summary
public static <U> Matcher<Iterable<? extends U>> everyItem(Matcher<U> itemMatcher)

Creates a matcher for Iterables 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")))
Parameters
itemMatcher:Matcher<U>

the matcher to apply to every item provided by the examined Iterable

hasItemback to summary
public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher)

Creates a matcher for Iterables 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")))
Parameters
itemMatcher:Matcher<? super T>

the matcher to apply to items provided by the examined Iterable

hasItemback to summary
public static <T> Matcher<Iterable<? super T>> hasItem(T item)

Creates a matcher for Iterables 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"))
Parameters
item:T

the item to compare against the items provided by the examined Iterable

hasItemsback to summary
public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers)

Creates a matcher for Iterables 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")))
Parameters
itemMatchers:Matcher<? super T>[]

the matchers to apply to items provided by the examined Iterable

Annotations
@SafeVarargs
hasItemsback to summary
public static <T> Matcher<Iterable<T>> hasItems(T... items)

Creates a matcher for Iterables 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"))
Parameters
items:T[]

the items to compare against the items provided by the examined Iterable

Annotations
@SafeVarargs
instanceOfback to summary
public static <T> Matcher<T> instanceOf(Class<?> type)

Creates a matcher that matches when the examined object is an instance of the specified type, as determined by calling the java.lang.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));
isback to summary
public static <T> Matcher<T> is(Matcher<T> matcher)

Decorates another Matcher, retaining its behaviour, but allowing tests to be slightly more expressive. For example:

assertThat(cheese, is(equalTo(smelly)))
instead of:
assertThat(cheese, equalTo(smelly))
isback to summary
public static <T> Matcher<T> is(T value)

A shortcut to the frequently used is(equalTo(x)). For example:

assertThat(cheese, is(smelly))
instead of:
assertThat(cheese, is(equalTo(smelly)))
isAback to summary
public static <T> Matcher<T> isA(Class<T> type)

A shortcut to the frequently used is(instanceOf(SomeClass.class)). For example:

assertThat(cheese, isA(Cheddar.class))
instead of:
assertThat(cheese, is(instanceOf(Cheddar.class)))
notback to summary
public 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. For example:

assertThat(cheese, is(not(equalTo(smelly))))
Parameters
matcher:Matcher<T>

the matcher whose sense should be inverted

notback to summary
public static <T> Matcher<T> not(T value)

A shortcut to the frequently used not(equalTo(x)). For example:

assertThat(cheese, is(not(smelly)))
instead of:
assertThat(cheese, is(not(equalTo(smelly))))
Parameters
value:T

the value that any examined object should not equal

notNullValueback to summary
public static Matcher<Object> notNullValue()

A shortcut to the frequently used not(nullValue()). For example:

assertThat(cheese, is(notNullValue()))
instead of:
assertThat(cheese, is(not(nullValue())))
notNullValueback to summary
public static <T> Matcher<T> notNullValue(Class<T> type)

A shortcut to the frequently used 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))))
Parameters
type:Class<T>

dummy parameter used to infer the generic type of the returned matcher

nullValueback to summary
public static Matcher<Object> nullValue()

Creates a matcher that matches if examined object is null. For example:

assertThat(cheese, is(nullValue())
nullValueback to summary
public static <T> Matcher<T> nullValue(Class<T> type)

Creates a matcher that matches if examined object is null. Accepts a single dummy argument to facilitate type inference. For example:

assertThat(cheese, is(nullValue(Cheese.class))
Parameters
type:Class<T>

dummy parameter used to infer the generic type of the returned matcher

sameInstanceback to summary
public 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.

Parameters
target:T

the target instance against which others should be assessed

startsWithback to summary
public static Matcher<String> startsWith(String prefix)

Creates a matcher that matches if the examined String starts with the specified String.

For example:
assertThat("myStringOfNote", startsWith("my"))
Parameters
prefix:String

the substring that the returned matcher will expect at the start of any examined string

startsWithIgnoringCaseback to summary
public static Matcher<String> startsWithIgnoringCase(String prefix)

Creates a matcher that matches if the examined String starts with the specified String, ignoring case

For example:
assertThat("myStringOfNote", startsWith("my"))
Parameters
prefix:String

the substring that the returned matcher will expect at the start of any examined string

theInstanceback to summary
public 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.

Parameters
target:T

the target instance against which others should be assessed