使用方法指针运算符和MockFor

时间:2017-07-28 00:31:11

标签: groovy

我做了一个测试。它使用了mockFor并且正在工作,我很高兴。直到某些东西改变了,我被迫使用方法指针操作符,然后我的快乐只是一个记忆。

这是一个问题的受限制的例子

import groovy.mock.interceptor.*

// I am testing againts a interface, not an actual class
// this is not negociable.
interface Person {
    void sayHello(name);
}

// Setup (can do whatever I need)
def personMock = new MockFor(Person)
personMock.demand.sayHello { name -> println "Hello $name" }
def person = personMock.proxyInstance()
// End Setup

// Exercise (can not change)
def closureMethod = person.&sayHello.curry("Groovy!")
closureMethod()
// End Exercise

personMock.verify(person)

哪种方法是最安全,最简单的方法来修复测试? 目前,测试失败并显示java.lang.UnsupportedOperationException

1 个答案:

答案 0 :(得分:0)

即使我正在测试一个接口,也没有什么能阻止我创建一个实现接口的模拟类,并且做一个穷人验证需求。

import groovy.mock.interceptor.*

// I am testing against a interface, not an actual class
// this is not negociable.
interface Person {
    void sayHello(name);
}

class PersonMock implements Person {
    def calls = []
    void sayHello(name) { calls << "sayHello" }
}

// Setup (can do whatever I need)
def person = new PersonMock()
// End Setup

// Exercise (can not change)
def closureMethod = person.&sayHello.curry("Groovy!")
closureMethod()
// End Exercise

assert person.calls == ['sayHello']
相关问题