jmock 模拟静态方法

发布于 2024-07-06 19:33:35 字数 237 浏览 8 评论 0原文

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

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我要还你自由 2024-07-13 19:33:36

我们不支持在 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.

爱已欠费 2024-07-13 19:33:36

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

在设计其他面向对象的解决方案时,我认为明智地使用静态方法没有任何问题。

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


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

其中 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:


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

where the find method is statically imported from a PersistenceFacade 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.

憧憬巴黎街头的黎明 2024-07-13 19:33:36

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

Powermock is an extension to EasyMock that allows mocking of static methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文