2014-02-13 53 views
0

我有以下代碼:如何使用JMockit模擬接受Closure參數的Groovy方法?

def method() { 
    try { 
     dependency0.call({ arg -> }) 
    } catch { 
     dependency1.call() 
    } 
} 

和下面的測試:

@Test 
void shouldDoSomething(
     @Mocked final Dependency0 dependency0Mock) { 
    final dependency1Mock = getDependency1Mock() 

    new Expectations() {{ 
     dependency0Mock.call((Closure) any) 
     result = new Exception('expected') 

    final sut = new Sut(dependency1Mock, dependency0Mock) 

    sut.method() 
} 

當測試運行時,會發出以下異常:

mockit.internal.UnexpectedInvocation: Parameter "arg" of Dependency0#call(groovy.lang.Closure arg) expected null, got [email protected] 

我怎麼去嘲笑採用Groovy Closure的方法?

更新:即使參數類型爲Integer也會引發同樣的異常。

回答

相關問題