我需要模拟一个静态方法.我正在使用使用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中没有参数的模拟静态方法
发布评论