1

我正在使用 gMock for groovy 创建单元测试。我有一个基类,其中包含一个“玩家”的 ArrayList 我有另一个类用作我的 GUI 界面。该用户界面中的一个方法将获取玩家列表并在组合框中显示玩家名称。该方法如下所示:

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(buchwalter.oldschoolgame.panels.MainMenuPanel$_displayGuild_closure2@4f2f2008)' on 'Mock for ArrayList'
  'setGuildList([buchwalter.oldschoolgame.characters.Fighter@501b7924, buchwalter.oldschoolgame.characters.Barbarian@3bae5392])' 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)

任何帮助,将不胜感激

4

1 回答 1

0

您应该能够通过使用一个简单的自定义匹配器来快速修复您的测试,该匹配器匹配传递给 collect 方法的任何闭包,通过以下方式调整一行:

mockList.collect(match { return true }).returns(playerList.collect(){obj  -> obj.toString()})
于 2014-03-18T23:48:20.403 回答