I'm really happy to say that I've just released a new version 1.0.1 of the Spock Subject Collaborators Extension.

The changelog is as follows:


Bug fixes:
#3 Make plugin compatible with Spock 1.0.0-SNAPSHOT


New features:
#1 Inject superclass fields - now you can inject fields to your superclass

As you can see now you'll be able to use this extension together with Spock in version 1.0.0 (assuming that nothing will change until then).

How to get it?

For Maven:

Add JCenter repository:
Add dependency:

For Gradle:

Add JCenter repository:
repositories {
Add dependency:
dependencies {
testCompile 'com.blogspot.toomuchcoding:spock-subjects-collaborators-extension:1.0.1'

How to use it?

Below you have an example of usage:

package com.blogspot.toomuchcoding.spock.subjcollabs

import spock.lang.Specification
import com.blogspot.toomuchcoding.spock.subjcollabs.Collaborator
import com.blogspot.toomuchcoding.spock.subjcollabs.Subject

class ConstructorInjectionSpec extends Specification {

public static final String TEST_METHOD_1 = "Test method 1"

SomeOtherClass someOtherClassNotToBeInjected = Mock()

SomeOtherClass someOtherClass = Mock()

SomeClass systemUnderTest

def "should inject collaborator into subject"() {
someOtherClass.someMethod() >> TEST_METHOD_1

String firstResult = systemUnderTest.someOtherClass.someMethod()

firstResult == TEST_METHOD_1
systemUnderTest.someOtherClass == someOtherClass

class SomeClass {
SomeOtherClass someOtherClass

SomeClass(SomeOtherClass someOtherClass) {
this.someOtherClass = someOtherClass

class SomeOtherClass {
String someMethod() {
"Some other class"



Remember that if you're using this extension as a way to hack your way through an awful design of your application then you should do your best to fix your code in the first place! You've been warned ;)