为什么 Spock 不记录通过间谍对象的方法完成的调用?

Why does Spock not record calls done by a method of a spied object?

我正在 Spock 框架的帮助下编写一个基于交互的测试,我想在其中检查一个方法是否调用了另一个方法一次。

测试中的 class 看起来像这样:

class ClassUnderTest {
    def a() {
        b()
    }

    def b() {}
}

我的测试用例是这样的

class ClassUnderTestTest extends Specification {
    ClassUnderTest cut
    ClassUnderTest spied

    def setup() {
        cut = new ClassUnderTest()
        spied = Spy(cut)
    }

    def "Method a() calls method b()"() {
        when:
        spied.a()

        then:
        1 * spied.a()
        1 * spied.b()
    }
}

运行 这个测试,Spock 只识别对 a() 的调用,而不识别对 b().

的调用

我做错了什么或错过了什么?

你的代码有效,确保你有最新版本的 spock-core 当前版本是 2.0-groovy-3.0,你还需要 byte-buddy 来模拟 类 而不是仅仅接口。