Mocking and Lists
This lesson gives an overview of the mocking interface, lists, and exceptions in Spock.
We'll cover the following...
Mocking
Mocking interfaces is extremely easy in Spock. Simply use the Mock method, as shown in the following example (where Subscriber is an interface):
class APublisher extends Specification {
def publisher = new Publisher()
def subscriber = Mock(Subscriber)
Now subscriber
is a mocked object. We can implement methods simply using the overloaded operator >>
as shown below.
def "can cope with misbehaving subscribers"() {
subscriber.receive(_) >> { throw new Exception() }
when:
publisher.send("event")
publisher.send("event")
then:
...