Groovy中没有参数的模拟静态方法

编程入门 行业动态 更新时间:2024-10-25 08:15:51
本文介绍了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中没有参数的模拟静态方法

本文发布于:2023-10-31 04:04:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   参数   方法   Groovy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!