Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelbannister/afce6c6a5ad22649ffea to your computer and use it in GitHub Desktop.
Save michaelbannister/afce6c6a5ad22649ffea to your computer and use it in GitHub Desktop.
Spock Subjects-Collaborators extension not happy with stubbed method on Spy subject
package com.blogspot.toomuchcoding.spock.subjcollabs
import com.blogspot.toomuchcoding.spock.subjcollabs.Collaborator
import com.blogspot.toomuchcoding.spock.subjcollabs.Subject
import spock.lang.Specification
class SettersInjectionOnSpySpec extends Specification {
public static final String TEST_METHOD_1 = "Test method 1"
@Collaborator
SomeOtherClass someOtherClassInjected = Mock()
@Subject
SomeClass systemUnderTestInjected = Spy()
SomeOtherClass someOtherClassPlain = Mock()
SomeClass systemUnderTestPlain = Spy()
def "should inject collaborator into subject without disrupting spy stubbing"() {
given:
someOtherClassInjected.getResult() >> 'mock called'
systemUnderTestInjected.throwException() >> {}
when:
def result = systemUnderTestInjected.collaborate()
then:
result == 'mock called'
}
def "prove that stubbing method on a spy normally works"() {
given:
systemUnderTestPlain.collaborator = someOtherClassPlain
and:
someOtherClassPlain.getResult() >> 'mock called'
systemUnderTestPlain.throwException() >> {}
when:
def result = systemUnderTestPlain.collaborate()
then:
result == 'mock called'
}
class SomeClass {
SomeOtherClass collaborator
String collaborate() {
throwException()
return collaborator.getResult()
}
void throwException() {
throw new RuntimeException()
}
}
class SomeOtherClass {
def getResult() {
return 'SomeOtherClass called'
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment