spock.util.matcher
public class HamcrestSupport extends Object
Constructor and Description |
---|
HamcrestSupport() |
Modifier and Type | Method and Description |
---|---|
static <T> void |
expect(T value,
Matcher<? super T> matcher)
Alias for
that(Object, org.hamcrest.Matcher) intended for use in then-blocks. |
static <T> void |
that(T value,
Matcher<? super T> matcher)
Used to match a value against a (Hamcrest) matcher.
|
public static <T> void that(T value, Matcher<? super T> matcher)
Basic example:
import static spock.util.matcher.HamcrestSupport.that import static org.hamcrest.CoreMatchers.equalTo // ships with JUnit def foo = 42 expect: that(foo, equalTo(42))Note that Spock supports an even simpler syntax for applying matchers:
expect: foo equalTo(42)However, the simpler syntax cannot be used in explicit conditions (i.e. after the 'assert' keyword), and may not be as IDE-friendly. That's why this method is provided as an alternative.
Due to Spock's good diagnostic messages and Groovy's expressivity, matchers are less often needed than when, say, writing JUnit tests in Java. However, they come in handy when more complex conditions are required (and possibly repeated throughout a project). In such cases, Spock's Hamcrest integration provides the best of two worlds: the diagnostic messages known from Spock's conditions, and the custom failure messages of Hamcrest matchers.
The matchers that ship with JUnit aren't very useful per se. Instead, you will want to use matchers from Hamcrest (http://code.google.com/p/hamcrest/) or other libraries. Both Hamcrest 1.1 and 1.2 are supported. You can also write your own matchers, building up a matcher library that's specific to the needs of your project.
T
- the actual value's typevalue
- the actual valuematcher
- a matcher describing the expected valuepublic static <T> void expect(T value, Matcher<? super T> matcher)
that(Object, org.hamcrest.Matcher)
intended for use in then-blocks.T
- the actual value's typevalue
- the actual valuematcher
- a matcher describing the expected value