The mocked class is defined as follows:
interface SomeInterface {
val somethingCommon: String
}
class SomeClass(val somethingSpecific: String) : SomeInterface {
override val somethingCommon: String
get() = somethingSpecific
}
The code under test mocks SomeClass and internally uses both the specific property, and the common interface. Unfortunately mocking just the specific property does not mock the associated interface method so it seems necessary to mock both:
def thing = Mock(SomeClass)
thing.somethingSpecific >> "blah"
thing.somethingCommon >> "blah"
Is there a way in Kotlin/Groovy/Spock to avoid having to stub both methods? The best I have come up with is to stub one with the other, which works, but is unfortunate:
def thing = Mock(SomeClass)
thing.somethingSpecific >> "blah"
thing.somethingCommon >> thing.somethingSpecific
CodePudding user response:
The behavior you are describing is a Spy, a Mock will return null
if you haven't specified any return values.
Spock can only mock non-final classes/methods, but you can use https://github.com/joke/spock-mockable to dynamically open them for testing.