Skip to content

Instantly share code, notes, and snippets.

@fmamud
Last active October 27, 2016 02:18
Show Gist options
  • Save fmamud/3415a0b7f6f03bc30d8020b0b3d6a31f to your computer and use it in GitHub Desktop.
Save fmamud/3415a0b7f6f03bc30d8020b0b3d6a31f to your computer and use it in GitHub Desktop.
Mock and Spy in Spock Specification
import spock.lang.*
class MySpec extends Specification {
def starshipName = 'NCC-1701'
def "size method should be executed three times"() {
given:
List list = Mock()
when:
list.add(1)
list.add 2
list << 3
then:
0 * list.add(!null)
3 * list.add(_ as Integer)
}
def "should taking off starship and print text to console"() {
given:
Launchable launchable = Stub()
OffService service = Spy(USSEnterprise, constructorArgs: [launchable])
when:
service.takeOff(new Starship(name: 'NCC-1701'))
then:
1 * service.takeOff({ it.name == starshipName })
}
}
interface Launchable { }
interface OffService {
void takeOff(Starship starship)
}
class Starship {
String name
}
class USSEnterprise implements OffService {
USSEnterprise(Launchable launchable) { }
@Override
void takeOff(Starship starship) {
println "${starship.name} taking off..."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment