jmock模拟静态方法

编程入门 行业动态 更新时间:2024-10-26 04:25:53
本文介绍了jmock模拟静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的代码中有一个静态方法,我想以某种方式模拟。

I have a static method in my code that I would like somehow to mock.

我正在使用jmock。

I am using jmock.

我认为我可以做到这一点的一种方法是在静态方法周围使用包装类并且模拟这个,但我希望有更好的解决方案。

One way I suppose I could do this is to have "wrapper class" around the static method and mock this but I was hoping for a better solution.

我的方式错了吗?

反馈:

我本来有一个接口和类,它有一个只调用静态方法的方法。它允许我通过模拟对这个包装类的调用来模拟逻辑。 (即使谈论它我也觉得很脏:))

I was going to have a interface and class that had a method that just called the static method. It would allow me to mock the logic by just mocking the call to this wrapper class. (I feel dirty even talking about it :) )

推荐答案

我们不支持在jMock中模拟静态方法,因为它没有'适合我们的设计方法。我们不希望将静态方法用于可能影响系统状态的重要功能。我们倾向于仅使用它们来支持OO代码并使其更具可读性。这就是为什么我们将静态方法模拟为存在问题的提示。一个例外是它在第三方库中的位置,但我们可能会将它包装在更加面向对象的东西中。

We don't support mocking static methods in jMock because it doesn't fit our design approach. We prefer not to use static methods for significant features that can affect the state of the system. We tend to use them just to support the OO code and make it more readable. That's why we view mocking a static methods as a hint that there's a problem. One exception is where it's in a third-party library, but we would probably wrap that in something more object-oriented anyway.

更多推荐

jmock模拟静态方法

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

发布评论

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

>www.elefans.com

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