【问题标题】:Spock throws MissingMethodException on dynamic method that is not presented in interfaceSpock 在接口中未显示的动态方法上抛出 MissingMethodException
【发布时间】:2015-08-05 10:07:35
【问题描述】:

我有扩展的 groovy.lang.GroovyObject 接口。实现类没有公共构造函数,并且包含接口中没有呈现的动态方法。

我正在尝试这样做:

def bean = Stub(GroovyObject)
bean.getResults() >> ['result1', 'results2']

调用时:

bean.getResults() 

它会抛出 groovy.lang.MissingMethodException。 其实我并没有进行接口契约,我只需要确保 stubed 对象返回期望列表。

我也不能存根实现类,它会抛出CannotCreateMockException。

【问题讨论】:

  • 你有显示失败的可运行示例吗?
  • 尝试在规范测试中运行这个 def bean = Stub(GroovyObject) bean.getResults() >> ['result1', 'results2'] bean.getResults() 你会得到 No signature of方法....@Tim
  • 您要删除的类/接口是什么?你为什么要存根 GroovyObject 而不是你的接口?
  • 我没有接口。事实上,最终对象是一个 GroovyMBean,它根据来自 jmx @tim_yates 的内容在运行时绑定方法

标签: grails groovy spock


【解决方案1】:

看来我自己终于找到了解决办法。

必须使用 GroovyStub 而不是 Stub。这将不允许对存根类的方法进行验证。 GroovyObject 接口也不起作用,必须使用 GroovyObjectSupport 抽象类:

def bean = GroovyStub(GroovyObjectSupport) 
bean.getResults() >> ['result1', 'results2']
assert bean.getResults() == ['result1', 'results2']

【讨论】:

    猜你喜欢
    • 2015-04-17
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多