2014-03-01 69 views
1

我使用gMock進行groovy創建單元測試。我有一個基類,其中包含「玩家」的ArrayList我有另一個類作爲我的GUI界面。該用戶界面中的一種方法將獲取玩家列表並在組合框中顯示玩家名稱。該方法是這樣的:如何使用gMock模擬一個包含閉包的方法以實現groovy?

def displayGuild(){ 
    guildList = new JList(g.guild.collect(){ obj -> obj.getName()}) 
} 

我有試圖設定預期,此方法的返回值是我的問題是這樣的:

... 
    def playerList = getGuildList() 
    mockGame.setGuildList(playerList).returns(playerList) 
    mockGame.guild.returns(mockList).times(2) 
    mockList.isEmpty().returns(false) 
    mockList.collect().returns(playerList.collect(){obj -> obj.getName()}) 
    festSetUp(panel.gPane.mainM) 
    play{ 
     panel.g.setGuildList(playerList) 
     robot.keyPress(VK_A) 
     robot.keyRelease(VK_A) 
     assertThat (panel.gPane.addCharacter.isVisible(), is(true)) 
    } 

當我運行測試我得到了一個體面的堆棧跟蹤,但我不知道如何解決它,因爲它看起來問題是關閉。

我的縮寫堆棧跟蹤是這樣的:

Exception in thread "AWT-EventQueue-0" junit.framework.AssertionFailedError: Unexpected method call 'collect(buchw[email protected]4f2f2008)' on 'Mock for ArrayList' 
    'setGuildList([[email protected], [email protected]])' on 'Mock for Game': expected 1, actual 1 
    'guild' on 'Mock for Game': expected 2, actual 2 
'isEmpty()' on 'Mock for ArrayList': expected 1, actual 1 
    'collect()' on 'Mock for ArrayList': expected 1, actual 0 
at junit.framework.Assert.fail(Assert.java:57) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90) 
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) 
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrap.invoke(StaticMetaMethodSite.java:130) 
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:88) 
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) 
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) 
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) 
at org.gmock.internal.InternalMockController.fail(InternalMockController.groovy:171) 
at org.gmock.internal.InternalMockController$fail.call(Unknown Source) 
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) 
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) 
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120) 
at org.gmock.internal.metaclass.MetaClassHelper.findExpectation(MetaClassHelper.groovy:25) 
at org.gmock.internal.metaclass.MetaClassHelper$findExpectation.callStatic(Unknown Source) 
at org.gmock.internal.MockInternal.invokeMockMethod(MockInternal.groovy:84) 
at org.gmock.internal.metaclass.MockProxyMetaClass$2.call(MockProxyMetaClass.java:51) 
at org.gmock.internal.Callable$call.call(Unknown Source) 
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) 
at org.gmock.internal.Callable$call.call(Unknown Source) 
at org.gmock.internal.InternalMockController.doWork(InternalMockController.groovy:204) 
at org.gmock.internal.InternalMockController.this$2$doWork(InternalMockController.groovy) 
at org.gmock.internal.InternalMockController$this$2$doWork.callCurrent(Unknown Source) 
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49) 
at org.gmock.internal.InternalMockController$this$2$doWork.callCurrent(Unknown Source) 
at org.gmock.internal.InternalMockController.doInternal(InternalMockController.groovy:193) 
at org.gmock.internal.InternalMockController$doInternal.callCurrent(Unknown Source) 
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49) 
at org.gmock.internal.InternalMockController$doInternal.callCurrent(Unknown Source) 
at org.gmock.internal.InternalMockController.doInternal(InternalMockController.groovy:186) 
at org.gmock.internal.metaclass.MockProxyMetaClass.invokeMethod(MockProxyMetaClass.java:45) 
at org.gmock.internal.metaclass.MockProxyMetaClass.invokeMethod(MockProxyMetaClass.java:40) 
at org.gmock.internal.metaclass.GeneratedMockProxyMetaClass.invokeMethod(GeneratedMockProxyMetaClass.java:37) 

任何幫助,將不勝感激

+0

您最近在這個問題上工作過嗎?你設法解決它嗎? – papacito

回答

0

您應該能夠通過使用簡單的定製匹配,快速解決您的測試中,傳遞給收集任何封閉匹配方法,通過這樣調整一條線:

mockList.collect(match { return true }).returns(playerList.collect(){obj -> obj.toString()}) 
相關問題