jmock mocking a static method(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 模拟静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jmock 模拟静态方法


基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01