public class HamcrestSupport extends Object
|Constructor and Description|
|Modifier and Type||Method and Description|
Used to match a value against a (Hamcrest) matcher.
public static <T> void that(T value, Matcher<? super T> matcher)
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 type
value- the actual value
matcher- a matcher describing the expected value