/spock-deepmock

Deep mocking for the Spock Framework

Primary LanguageJavaApache License 2.0Apache-2.0

spock-deepmock

badge GitHub spock deepmock?label=latest%20version Conventional%20Commits 1.0.0 yellow pre-commit

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() and GroovyDeepMock() for deep mocking

  • Build deep mocking chains for nested objects

  • Uses same syntax as the builtins Mock() and GroovyMock()

  • Supports interaction verification

  • Works with Spock Framework 2.0, 2.1, 2.2 and 2.3

Gradle Dependency

spock deepmock?label=latest%20version

build.gradle
dependencies {
    testImplementation 'io.github.joke:spock-deepmock:x.y.z'
}

Maven Dependency

pom.xml
<depenencies>
  <dependency>
    <groupId>io.github.joke</groupId>
    <artifactId>spock-deepmock</artifactId>
    <version>x.y.z</version>
    <scope>test</scope>
  </dependency>
</depenencies>

Usage

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.