spock-deepmock
adds deep mocking capabilities to the Spock Framework.
In contrast to builtin mocks a deep mock will return another deep mock object automatically. This way there is no need to define chains of nested mocks.
-
Adds
DeepMock()
andGroovyDeepMock()
for deep mocking -
Build deep mocking chains for nested objects
-
Uses same syntax as the builtins
Mock()
andGroovyMock()
-
Supports interaction verification
-
Works with Spock Framework 2.0, 2.1, 2.2 and 2.3
pom.xml
<depenencies>
<dependency>
<groupId>io.github.joke</groupId>
<artifactId>spock-deepmock</artifactId>
<version>x.y.z</version>
<scope>test</scope>
</dependency>
</depenencies>
Deep mocks can be defined as any other mock. Features like options are supported.
Define deep mocks
def mock = DeepMock(Nested)
Nested mock = DeepMock()
def mock = DeepMock(name: 'customName')
Nested mock = DeepMock(name: 'customName')
CallerTest.groovy
def 'calling nested mock'() {
setup:
Nested mock = DeepMock()
def caller = new Caller(mock)
when:
// calls mock.getChild().getChild().getName()
def res = caller.nameOfSubSubChild()
then:
1 * mock.child.child.name >> 'Hello'
expect:
res == 'Hello'
}
Further examples in examples-java or examples-groovy.