Created
April 2, 2015 20:06
-
-
Save michaelbannister/afce6c6a5ad22649ffea to your computer and use it in GitHub Desktop.
Spock Subjects-Collaborators extension not happy with stubbed method on Spy subject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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