2017-07-28 18 views
0

我有一個測試。它正在使用mockFor並正在工作,我很高興。在某些事情改變之前,我不得不使用方法指針操作符,然後我的快樂只是一個記憶。UnsupportedOperationException當使用方法指針運算符和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

回答

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']