Used to match a value against a (Hamcrest) matcher.
Only allowed in places where a condition is expected
(expect-block, then-block, after an 'assert' keyword).
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.
When would I use matchers?
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.
Third-party 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.
- Type Parameters:
T
- the value's type- Parameters:
value
- an actual valuematcher
- a matcher describing the expected value(s)