Commit
DON'T DO THIS AT HOME!
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package com.blogspot.toomuchcoding.spock.subjcollabs | ||
|
||
import groovy.transform.PackageScope | ||
import org.spockframework.runtime.model.FieldInfo | ||
import spock.lang.Specification | ||
|
||
import java.lang.reflect.Field | ||
|
||
|
||
@PackageScope | ||
interface Injector { | ||
boolean tryToInject(Collection<Field> injectionCandidates, Specification specInstance, FieldInfo fieldInfo) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.blogspot.toomuchcoding.spock.subjcollabs | ||
import spock.lang.Specification | ||
|
||
import java.lang.reflect.Field | ||
|
||
class PropertiesInjectionWithSuperclassSpec extends Specification { | ||
|
||
public static final String TEST_METHOD_1 = "Test method 1" | ||
|
||
public static final String TEST_METHOD_2 = "Test method 2" | ||
|
||
@Collaborator | ||
SomeOtherClass someOtherClassToBeInjected = Mock() | ||
|
||
@Collaborator | ||
SomeOtherClass someOtherClass = Mock() | ||
|
||
@Subject | ||
SomeClass systemUnderTest | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
marcingrzejszczak
Author
Owner
|
||
|
||
def "should inject collaborator into subject via properties"() { | ||
given: | ||
Field someOtherClassField = systemUnderTest.class.superclass.declaredFields.find {it.name == 'someOtherClass'} | ||
This comment has been minimized.
Sorry, something went wrong.
szpak
Contributor
|
||
Field someOtherClassToBeInjectedField = systemUnderTest.class.superclass.declaredFields.find {it.name == 'someOtherClassToBeInjected'} | ||
someOtherClassField.accessible = true | ||
someOtherClassToBeInjectedField.accessible = true | ||
|
||
and: | ||
SomeOtherClass injectedSomeOtherClass = someOtherClassField.get(systemUnderTest) | ||
SomeOtherClass injectedSomeOtherClassToBeInjected = someOtherClassToBeInjectedField.get(systemUnderTest) | ||
|
||
and: | ||
someOtherClass.someMethod() >> TEST_METHOD_1 | ||
someOtherClassToBeInjected.someMethod() >> TEST_METHOD_2 | ||
|
||
when: | ||
String firstResult = injectedSomeOtherClass.someMethod() | ||
String secondResult = injectedSomeOtherClassToBeInjected.someMethod() | ||
|
||
then: | ||
firstResult == TEST_METHOD_1 | ||
injectedSomeOtherClass == someOtherClass | ||
|
||
and: | ||
secondResult == TEST_METHOD_2 | ||
injectedSomeOtherClassToBeInjected == someOtherClassToBeInjected | ||
} | ||
|
||
|
||
class SomeClassParent { | ||
private SomeOtherClass someOtherClass | ||
private SomeOtherClass someOtherClassToBeInjected | ||
} | ||
|
||
class SomeClass extends SomeClassParent { | ||
} | ||
|
||
class SomeOtherClass { | ||
String someMethod() { | ||
"Some other class" | ||
} | ||
} | ||
|
||
} | ||
|
||
|
Respect ;).