-
-
Save zanthrash/9a95b4e970f42171851c to your computer and use it in GitHub Desktop.
Spock @use annotation for partial mocking of CUT
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
class ClassUnderTest { | |
def index() { } | |
def doSomething() { | |
return mockMethod('Jojo') | |
} | |
private def mockMethod(String name) { | |
//...something expensive | |
return "real method called for $name" | |
} | |
} | |
class FakeClassUnderTest { | |
// we can alos use this as a spy | |
static boolean fakeCalled = false | |
static mockMethod(ClassUnderTest self, String name) { | |
fakeCalled = true | |
return "fake mock method called for $name" | |
} | |
} | |
class ClassUnderTestSpec extends Specification{ | |
@Use(FakeClassUnderTest) | |
def "this is to test the @Use annotation"() { | |
given: "we have a new CUT" | |
cut = new ClassUnderTest() | |
when:"call a method that calls the fake method" | |
def result = cut.doSomething() | |
then:"check results" | |
result == "fake mock method called for Bob" | |
FakeClassUnderTest.fakeCalled == true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment