jmock 模拟静态方法
我的代码中有一个静态方法,我想以某种方式模拟它。
我正在使用jmock。
我想我可以做到这一点的一种方法是在静态方法周围使用“包装类”并且 嘲笑这个,但我希望有更好的解决方案。
我以错误的方式处理这个问题吗?
反馈:
我将有一个接口和类,其中有一个仅调用静态方法的方法。 它允许我通过模拟对此包装类的调用来模拟逻辑。 (即使谈论它我也觉得很肮脏:))
I have a static method in my code that I would like somehow to mock.
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 am going about this the wrong way?
FEEDBACK:
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 :) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们不支持在 jMock 中模拟静态方法,因为它不适合我们的设计方法。 我们不喜欢对可能影响系统状态的重要功能使用静态方法。 我们倾向于使用它们只是为了支持面向对象代码并使其更具可读性。 这就是为什么我们将模拟静态方法视为存在问题的暗示。 一个例外是它位于第三方库中,但无论如何我们可能会将其包装在更面向对象的东西中。
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.
JMockit 是另一个允许模拟静态方法(以及最终方法、构造函数等)的工具包.)。
在设计其他面向对象的解决方案时,我认为明智地使用静态方法没有任何问题。
例如,我喜欢使用的一种模式/习惯是静态外观,特别是为业务应用程序中的持久性子系统提供更简单且更易于使用的 API。 在我看来,没有其他解决方案比这样更优雅:
其中
find
方法是从PersistenceFacade
类静态导入的,该类仅定义静态方法,并封装如何获取正确的 Session/EntityManager 实例。 该解决方案单元测试友好且灵活。 我在一个拥有 500 多个持久实体的业务应用程序中使用了它,使用 Hibernate。 当我们从 Hibernate 2 迁移到 Hibernate 3 时,当我们从 Oracle 迁移到 Sybase 然后再迁移回 Oracle 时,以及当我们开始使用 JPA 注释而不是“hbm.xml”文件进行 ORM 映射时,静态外观很有帮助。JMockit is another toolkit which allows mocking of static methods (as well as final methods, constructors, etc.).
I don't see any problem with the judicious use of static methods when designing an otherwise OO solution.
For example, one pattern/idiom I like to use is the static facade, particularly to provide a simpler and easier to use API to the persistence subsystem in a business application. In my opinion, no other solution is more elegant than something like:
where the
find
method is statically imported from aPersistenceFacade
class which defines only static methods, and encapsulates how to obtain the proper Session/EntityManager instance. This solution is unit-testing friendly and flexible. I used it in a business application which had 500+ persistent entities, using Hibernate. The static facade helped when we migrated from Hibernate 2 to Hibernate 3, when we migrated from Oracle to Sybase and then back to Oracle, and when we started using JPA annotations instead of "hbm.xml" files for ORM mapping.Powermock 是 EasyMock 的扩展,允许模拟静态方法。
Powermock is an extension to EasyMock that allows mocking of static methods.