类 CoreMatchers
- java.lang.Object
- org.hamcrest.CoreMatchers
public class CoreMatchers extends Object
构造器概要
构造器 构造器 说明 CoreMatchers()
方法概要
所有方法 静态方法 具体方法 已过时的方法 修饰符和类型 方法 说明 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.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 <T> Matcher<T>
any(Class<T> type)
Creates a matcher that matches when the examined object is an instance of the specifiedtype
, as determined by calling theClass.isInstance(Object)
method on that type, passing the the examined object.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.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<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<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<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<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<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<Object>
anything()
Creates a matcher that always matches, regardless of the examined object.static Matcher<Object>
anything(String description)
Creates a matcher that always matches, regardless of the examined object, but describes itself with the specifiedString
.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<String>
containsString(String substring)
static <T> Matcher<T>
describedAs(String description, Matcher<T> matcher, 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 Matcher<String>
endsWith(String suffix)
static <T> Matcher<T>
equalTo(T operand)
Creates a matcher that matches when the examined object is logically equal to the specifiedoperand
, as determined by calling theObject.equals(java.lang.Object)
method on the examined object.static <U> Matcher<Iterable<U>>
everyItem(Matcher<U> itemMatcher)
static <T> Matcher<Iterable<? super T>>
hasItem(Matcher<? super T> itemMatcher)
static <T> Matcher<Iterable<? super T>>
hasItem(T item)
static <T> Matcher<Iterable<T>>
hasItems(Matcher<? super T>... itemMatchers)
static <T> Matcher<Iterable<T>>
hasItems(T... items)
static <T> Matcher<T>
instanceOf(Class<?> type)
Creates a matcher that matches when the examined object is an instance of the specifiedtype
, as determined by calling theClass.isInstance(Object)
method on that type, passing the the examined object.static <T> Matcher<T>
is(Class<T> type)
已过时。use isA(Classtype) instead. 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 usedis(equalTo(x))
.static <T> Matcher<T>
isA(Class<T> type)
A shortcut to the frequently usedis(instanceOf(SomeClass.class))
.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 usednot(equalTo(x))
.static Matcher<Object>
notNullValue()
A shortcut to the frequently usednot(nullValue())
.static <T> Matcher<T>
notNullValue(Class<T> type)
A shortcut to the frequently usednot(nullValue(X.class)).
static Matcher<Object>
nullValue()
Creates a matcher that matches if examined object isnull
.static <T> Matcher<T>
nullValue(Class<T> type)
Creates a matcher that matches if examined object isnull
.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 Matcher<String>
startsWith(String prefix)
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.
构造器详细资料
CoreMatchers
public CoreMatchers()
方法详细资料
allOf
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")))
allOf
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")))
allOf
public 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. For example:assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
allOf
public 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. For example:assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
allOf
public 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. For example:assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
allOf
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)
Creates a matcher that matches if the examined object matches ALL of the specified matchers. For example:assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
allOf
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)
Creates a matcher that matches if the examined object matches ALL of the specified matchers. For example:assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
anyOf
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")))
anyOf
public static <T> AnyOf<T> anyOf(Matcher<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. For example:assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
anyOf
public static <T> AnyOf<T> anyOf(Matcher<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. For example:assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
anyOf
public static <T> AnyOf<T> anyOf(Matcher<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. For example:assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
anyOf
public static <T> AnyOf<T> anyOf(Matcher<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. For example:assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
anyOf
public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second)
Creates a matcher that matches if the examined object matches ANY of the specified matchers. For example:assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))
anyOf
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")))
both
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")))
either
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")).and(containsString("b")))
describedAs
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())
- 参数:
description
- the new description for the wrapped matchermatcher
- the matcher to wrapvalues
- optional values to insert into the tokenised description
everyItem
public static <U> Matcher<Iterable<U>> everyItem(Matcher<U> itemMatcher)
Creates a matcher forIterable
s that only matches when a single pass over the examinedIterable
yields items that are all matched by the specifieditemMatcher
. For example:assertThat(Arrays.asList("bar", "baz"), everyItem(startsWith("ba")))
- 参数:
itemMatcher
- the matcher to apply to every item provided by the examinedIterable
is
public static <T> Matcher<T> is(T value)
A shortcut to the frequently usedis(equalTo(x))
. For example:assertThat(cheese, is(smelly))
instead of:assertThat(cheese, is(equalTo(smelly)))
is
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))
is
public static <T> Matcher<T> is(Class<T> type)
已过时。use isA(Classtype) instead. A shortcut to the frequently usedis(instanceOf(SomeClass.class))
. For example:assertThat(cheese, is(Cheddar.class))
instead of:assertThat(cheese, is(instanceOf(Cheddar.class)))
isA
public static <T> Matcher<T> isA(Class<T> type)
A shortcut to the frequently usedis(instanceOf(SomeClass.class))
. For example:assertThat(cheese, isA(Cheddar.class))
instead of:assertThat(cheese, is(instanceOf(Cheddar.class)))
anything
public static Matcher<Object> anything()
Creates a matcher that always matches, regardless of the examined object.
anything
public static Matcher<Object> anything(String description)
Creates a matcher that always matches, regardless of the examined object, but describes itself with the specifiedString
.- 参数:
description
- a meaningfulString
used when describing itself
hasItem
public static <T> Matcher<Iterable<? super T>> hasItem(T item)
Creates a matcher forIterable
s that only matches when a single pass over the examinedIterable
yields at least one item that is equal to the specifieditem
. Whilst matching, the traversal of the examinedIterable
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 examinedIterable
hasItem
public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher)
Creates a matcher forIterable
s that only matches when a single pass over the examinedIterable
yields at least one item that is matched by the specifieditemMatcher
. Whilst matching, the traversal of the examinedIterable
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 examinedIterable
hasItems
public static <T> Matcher<Iterable<T>> hasItems(T... items)
Creates a matcher forIterable
s that matches when consecutive passes over the examinedIterable
yield at least one item that is equal to the corresponding item from the specifieditems
. Whilst matching, each traversal of the examinedIterable
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 examinedIterable
hasItems
public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers)
Creates a matcher forIterable
s that matches when consecutive passes over the examinedIterable
yield at least one item that is matched by the corresponding matcher from the specifieditemMatchers
. Whilst matching, each traversal of the examinedIterable
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 examinedIterable
equalTo
public static <T> Matcher<T> equalTo(T operand)
Creates a matcher that matches when the examined object is logically equal to the specifiedoperand
, as determined by calling theObject.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'sequals
method returnstrue
when passed anull
(which would be a violation of theequals
contract), unless the examined object itself isnull
, in which case the matcher will return a positive match.The created matcher provides a special behaviour when examining
For example: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"}));
any
public static <T> Matcher<T> any(Class<T> type)
Creates a matcher that matches when the examined object is an instance of the specifiedtype
, as determined by calling theClass.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
For example:with(any(Thing.class))
assertThat(new Canoe(), instanceOf(Canoe.class));
instanceOf
public static <T> Matcher<T> instanceOf(Class<?> type)
Creates a matcher that matches when the examined object is an instance of the specifiedtype
, as determined by calling theClass.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));
not
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))))
- 参数:
matcher
- the matcher whose sense should be inverted
not
public static <T> Matcher<T> not(T value)
A shortcut to the frequently usednot(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 equal
nullValue
public static Matcher<Object> nullValue()
Creates a matcher that matches if examined object isnull
. For example:assertThat(cheese, is(nullValue())
nullValue
public static <T> Matcher<T> nullValue(Class<T> type)
Creates a matcher that matches if examined object isnull
. 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 matcher
notNullValue
public static Matcher<Object> notNullValue()
A shortcut to the frequently usednot(nullValue())
. For example:assertThat(cheese, is(notNullValue()))
instead of:assertThat(cheese, is(not(nullValue())))
notNullValue
public static <T> Matcher<T> notNullValue(Class<T> type)
A shortcut to the frequently usednot(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 matcher
sameInstance
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.- 参数:
target
- the target instance against which others should be assessed
theInstance
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.- 参数:
target
- the target instance against which others should be assessed
containsString
public static Matcher<String> containsString(String substring)
Creates a matcher that matches if the examinedString
contains the specifiedString
anywhere. For example:assertThat("myStringOfNote", containsString("ring"))
- 参数:
substring
- the substring that the returned matcher will expect to find within any examined string
startsWith
public static Matcher<String> startsWith(String prefix)
Creates a matcher that matches if the examinedString
starts with the specifiedString
. For example:assertThat("myStringOfNote", startsWith("my"))
- 参数:
prefix
- the substring that the returned matcher will expect at the start of any examined string
endsWith
public static Matcher<String> endsWith(String suffix)
Creates a matcher that matches if the examinedString
ends with the specifiedString
. For example:assertThat("myStringOfNote", endsWith("Note"))
- 参数:
suffix
- the substring that the returned matcher will expect at the end of any examined string