T
- target typepublic abstract class TypeMatcher<T> extends org.hamcrest.TypeSafeMatcher<String>
Matcher
is base Matcher to verify
whether examined string value is convertible to the specified type
and whether converted value corresponds to the given value valueMatcher.
Examined string value can be evaluation of an XPath expression.
Currently BigDecimal
, Double
, Integer
and Boolean
types are supported.
Simple examples
assertThat("3.0", asDouble(greaterThanOrEqualTo(2.0))); assertThat("1.0e1", asBigDecimal(equalTo(BigDecimal.TEN))); assertThat("3", asInt(lessThan(4))); assertThat("false", asBoolean(equalTo(false))); assertThat("True", asBoolean(equalTo(true)));
Examples with XPath evaluation
String xml = "<fruits>" + "<fruit name=\"apple\"/>" + "<fruit name=\"orange\"/>" + "<fruit name=\"banana\"/>" + "<fruit name=\"pear\" fresh=\"false\"/>" + "</fruits>"; assertThat(xml, hasXPath("count(//fruits/fruit)", asDouble(equalTo(4.0)))); assertThat(xml, hasXPath("count(//fruits/fruit)", asBigDecimal(greaterThan(BigDecimal.ONE)))); assertThat(xml, hasXPath("count(//fruits/fruit)", asInt(lessThan(5)))); assertThat(xml, hasXPath("//fruits/fruit[@name=\"pear\"]/@fresh", asBoolean(equalTo(false))));
Constructor and Description |
---|
TypeMatcher(Class<T> clazz,
org.hamcrest.Matcher<? extends T> valueMatcher) |
Modifier and Type | Method and Description |
---|---|
static TypeMatcher<BigDecimal> |
asBigDecimal(org.hamcrest.Matcher<? extends BigDecimal> valueMatcher)
Creates a matcher that matches when the examined string is convertible to
BigDecimal
and converted value satisfies the specified valueMatcher . |
static TypeMatcher<Boolean> |
asBoolean(org.hamcrest.Matcher<? extends Boolean> valueMatcher)
Creates a matcher that matches when the examined string is convertible to
Boolean
and converted value satisfies the specified valueMatcher . |
static TypeMatcher<Double> |
asDouble(org.hamcrest.Matcher<? extends Double> valueMatcher)
Creates a matcher that matches when the examined string is convertible to
Double
and converted value satisfies the specified valueMatcher . |
static TypeMatcher<Integer> |
asInt(org.hamcrest.Matcher<? extends Integer> valueMatcher)
Creates a matcher that matches when the examined string is convertible to
Integer
and converted value satisfies the specified valueMatcher . |
protected abstract T |
convert(String item) |
protected void |
describeMismatchSafely(String item,
org.hamcrest.Description mismatchDescription) |
void |
describeTo(org.hamcrest.Description description) |
protected boolean |
matchesSafely(String item) |
protected boolean matchesSafely(String item)
matchesSafely
in class org.hamcrest.TypeSafeMatcher<String>
public void describeTo(org.hamcrest.Description description)
protected void describeMismatchSafely(String item, org.hamcrest.Description mismatchDescription)
describeMismatchSafely
in class org.hamcrest.TypeSafeMatcher<String>
public static TypeMatcher<BigDecimal> asBigDecimal(org.hamcrest.Matcher<? extends BigDecimal> valueMatcher)
BigDecimal
and converted value satisfies the specified valueMatcher
.
For example:
assertThat("1.0e1", asBigDecimal(equalTo(BigDecimal.TEN))); assertThat(xml, hasXPath("count(//fruits/fruit)", asBigDecimal(greaterThan(BigDecimal.ONE))));
valueMatcher
- valueMatcher for the converted valuepublic static TypeMatcher<Double> asDouble(org.hamcrest.Matcher<? extends Double> valueMatcher)
Double
and converted value satisfies the specified valueMatcher
.
For example:
assertThat("3.0", asDouble(greaterThanOrEqualTo(2.0))); assertThat(xml, hasXPath("count(//fruits/fruit)", asDouble(equalTo(3.0))));
valueMatcher
- valueMatcher for the converted valuepublic static TypeMatcher<Integer> asInt(org.hamcrest.Matcher<? extends Integer> valueMatcher)
Integer
and converted value satisfies the specified valueMatcher
.
For example:
assertThat("3", asInt(lessThan(4))); assertThat(xml, hasXPath("count(//fruits/fruit)", asInt(lessThan(4))));
valueMatcher
- valueMatcher for the converted valuepublic static TypeMatcher<Boolean> asBoolean(org.hamcrest.Matcher<? extends Boolean> valueMatcher)
Boolean
and converted value satisfies the specified valueMatcher
.
For example:
assertThat("false", asBoolean(equalTo(false))); assertThat(xml, hasXPath("//fruits/fruit[@name=\"apple\"]/@fresh", asBoolean(equalTo(true))));
valueMatcher
- valueMatcher for the converted valueCopyright © 2001–2020 XMLUnit. All rights reserved.