Groovy中没有参数的模拟静态方法 [英] Mock static method with no parameters in Groovy

查看:117
本文介绍了Groovy中没有参数的模拟静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要模拟一个静态方法.我正在使用使用groovy模拟静态方法中所述的EMC方法.像这样

TestDaemon.metaClass.'static'.newDownloadManager = {downloadManager}

newDownloadManager方法没有参数,由于某种原因它没有被替换.原始代码被调用.在调试模式下,我可以看到我定义的闭包具有一个参数.可能就是这个原因吗?如何定义不带参数的闭包?或者如何模拟没有参数的静态方法?

解决方案

元类更改对Java代码不可见. Groovy无法帮助您模拟从Java代码调用的静态方法.您将必须使用类似 JMockit 之类的东西(或重构受测试的代码).

I need to mock a static method. I'm using the EMC approach described at Mocking static methods using groovy. Like this

TestDaemon.metaClass.'static'.newDownloadManager = {downloadManager}

The method newDownloadManager has no parameters and for some reason it is not replaced. The original code is called. In debug mode I can see that the closure that I define has a parameter. May be that's the reason? How can I define a closure without parameters? Or how can I mock a static method with no parameters?

解决方案

Meta class changes aren't visible to Java code. Groovy can't help you to mock a static method that gets called from Java code. You will have to use something like JMockit instead (or refactor the code under test).

这篇关于Groovy中没有参数的模拟静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆