Last active
October 27, 2016 02:18
-
-
Save fmamud/3415a0b7f6f03bc30d8020b0b3d6a31f to your computer and use it in GitHub Desktop.
Mock and Spy in Spock Specification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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