Last active
March 24, 2023 17:58
-
-
Save craigatk/8994836 to your computer and use it in GitHub Desktop.
Spock Mocks Cheatsheet
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
def 'use method arguments in return value'() { | |
given: | |
String email = "test@email.com" | |
String name = "John Doe" | |
when: | |
userController.createUser(email, name) | |
then: | |
1 * userService.createUser(email, name) >> { String e, String n -> | |
new User(email: e, name: n) | |
} | |
1 * userService.sendWelcomeEmail(_ as User) | |
} |
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
dependencies { | |
compile 'org.codehaus.groovy:groovy-all:2.1.3' | |
testCompile 'org.spockframework:spock-core:0.7-groovy-2.0' | |
testCompile 'cglib:cglib-nodep:2.2' // For mocking classes | |
testCompile 'org.objenesis:objenesis:1.2' // For mocking final classes | |
} |
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
then: | |
1 * userService.createUser(_ as String, _ as String) >> user | |
1 * userService.sendWelcomeEmail(user) |
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
def 'closure-based argument matching'() { | |
given: | |
String email = "test@email.com" | |
String name = "John Doe" | |
when: | |
userController.createUser(email, name) | |
then: | |
1 * userService.createUser(email, name) >> new User(email: email, name: name) | |
1 * userService.sendWelcomeEmail({ User u -> u.email == email && u.name == name }) | |
} |
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
def 'exact argument matching'() { | |
given: | |
User user = new User() | |
String email = "test@email.com" | |
String name = "John Doe" | |
when: | |
userController.createUser(email, name) | |
then: | |
1 * userService.createUser(email, name) >> user | |
1 * userService.sendWelcomeEmail(user) | |
} |
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
@FailsWith(WrongInvocationOrderError) | |
def 'verify mock method call order - fail'() { | |
given: | |
User user = new User() | |
String email = "test@email.com" | |
String name = "John Doe" | |
when: | |
userController.createUser(email, name) | |
then: | |
1 * userService.sendWelcomeEmail(user) | |
then: | |
1 * userService.createUser(email, name) >> user | |
} |
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
def 'verify mock method call order - pass'() { | |
given: | |
User user = new User() | |
String email = "test@email.com" | |
String name = "John Doe" | |
when: | |
userController.createUser(email, name) | |
then: | |
1 * userService.createUser(email, name) >> user | |
then: | |
1 * userService.sendWelcomeEmail(user) | |
} |
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
def 'mock definition outside of test spec'() { | |
given: | |
String email = "test@email.com" | |
String name = "John Doe" | |
when: | |
userController.createUser(email, name) | |
then: | |
interaction { | |
userCreationMocks(email, name) | |
} | |
} | |
private void userCreationMocks(String email, String name) { | |
User user = new User() | |
1 * userService.createUser(email, name) >> user | |
1 * userService.sendWelcomeEmail(user) | |
} |
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
def userService = Mock(UserService) |
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
UserController userController | |
UserService userService | |
def setup() { | |
userService = Mock() | |
userController = new UserController( | |
userService: userService | |
) | |
} |
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
then: | |
1 * userService.createUser(email, name) >> user | |
1 * userService.sendWelcomeEmail(user) | |
0 * _ |
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
def 'wildcard-based method argument matching'() { | |
given: | |
User user = new User() | |
when: | |
userController.createUser("test@email.com", "John Doe") | |
then: | |
1 * userService.createUser(_, _) >> user | |
1 * userService.sendWelcomeEmail(user) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment