Groovy/Spock 可以在 Kotlin 中同时模拟方法和接口实现吗?

Can Groovy/Spock mock both a method and the interface implementation at the same time in Kotlin?

模拟的class定义如下:

interface SomeInterface {
    val somethingCommon: String
}
class SomeClass(val somethingSpecific: String) : SomeInterface {
    override val somethingCommon: String
      get() = somethingSpecific
}

被测代码模拟 SomeClass 并在内部使用特定 属性 和公共接口。不幸的是,仅模拟特定的 属性 不会模拟相关的接口方法,因此似乎有必要模拟两者:

def thing = Mock(SomeClass)
thing.somethingSpecific >> "blah"
thing.somethingCommon >> "blah"

Kotlin/Groovy/Spock 中有没有办法避免对这两种方法进行存根?我想出的最好办法是将一个与另一个存根,这可行,但不幸的是:

def thing = Mock(SomeClass)
thing.somethingSpecific >> "blah"
thing.somethingCommon >> thing.somethingSpecific

您描述的行为是 Spy, a Mock will return null if you haven't specified 任何 return 值。

Spock 只能模拟非最终 classes/methods,但您可以使用 https://github.com/joke/spock-mockable 动态打开它们进行测试。