Skip to content

Instantly share code, notes, and snippets.

@zanthrash
Created June 15, 2012 23:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zanthrash/9a95b4e970f42171851c to your computer and use it in GitHub Desktop.
Save zanthrash/9a95b4e970f42171851c to your computer and use it in GitHub Desktop.
Spock @use annotation for partial mocking of CUT
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