java jmock 模拟静态方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/218813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:28:35  来源:igfitidea点击:

jmock mocking a static method

javamockingjmock

提问by Paul Whelan

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

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

I am using jmock.

我正在使用 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 :) )

我将有一个接口和类,它有一个刚刚调用静态方法的方法。它允许我通过模拟对这个包装类的调用来模拟逻辑。(我什至在谈论它时都觉得很脏:))

采纳答案by Steve Freeman

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 中模拟静态方法,因为它不适合我们的设计方法。我们不喜欢对可能影响系统状态的重要特征使用静态方法。我们倾向于使用它们来支持 OO 代码并使其更具可读性。这就是为什么我们将模拟静态方法视为存在问题的暗示。一个例外是它位于第三方库中,但无论如何我们可能会将其包装在更面向对象的东西中。

回答by Rogério

JMockitis another toolkit which allows mocking of static methods (as well as final methods, constructors, etc.).

JMockit是另一个允许模拟静态方法(以及最终方法、构造函数等)的工具包。

I don't see any problem with the judicioususe 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:

例如,我喜欢使用的一种模式/习惯用法是静态外观,特别是为业务应用程序中的持久性子系统提供更简单、更易于使用的 API。在我看来,没有其他解决方案比以下解决方案更优雅:


    List<Person> peopleAboveAge = 
        find("select p from Person p where p.age >= ?", age);

where the findmethod is statically imported from a PersistenceFacadeclass 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.

其中find方法是从一个PersistenceFacade只定义静态方法的类静态导入的,并封装了如何获取正确的 Session/EntityManager 实例。该解决方案对单元测试友好且灵活。我在使用 Hibernate 的具有 500 多个持久实体的业务应用程序中使用了它。当我们从 Hibernate 2 迁移到 Hibernate 3、当我们从 Oracle 迁移到 Sybase 然后再返回到 Oracle 以及当我们开始使用 JPA 注释而不是“hbm.xml”文件进行 ORM 映射时,静态外观会有所帮助。

回答by Dónal

Powermockis an extension to EasyMock that allows mocking of static methods.

Powermock是 EasyMock 的扩展,允许模拟静态方法。