Mockito 与 JMockit 之间的比较 - 为什么 Mockito 比 JMockit 更好?
我正在调查我的项目使用哪个模拟框架,并将其范围缩小到 JMockit 和 Mockito。
我注意到 Mockito 被评为“最佳模拟框架Stackoverflow 上的 Java”。
比较 JMockit 的“模拟工具的功能比较矩阵”看起来JMockit有多个不同的功能。
是否有人对 Mockito 可以做什么而 JMockit 无法实现的事情有任何具体信息(不是意见),反之亦然?
I'm investigating which mocking framework to use for my project and have narrowed it down to JMockit and Mockito.
I notice that Mockito was voted "the best mock framework for Java" on Stackoverflow.
In comparing features on JMockit's "Mocking Tool Comparision Matrix" it appears that JMockit has multiple different features.
Does anyone have any specific information (not opinions) on what Mockito can do which can't be achieved with JMockit and vice versa?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
2019 年 9 月更新:唯一的 Spring Boot 支持(默认)的模拟框架是Mockito< /强>。如果你使用 Spring,答案就很明显了。
我想说竞争是在JMockit和PowerMock之间,然后是Mockito。
我会留下“普通”jMock 和 EasyMock,因为它们只使用代理和代理。 CGLIB 并且不像较新的框架那样使用 Java 5 工具。
jMock 也已经有 4 年多没有发布稳定版本了。 jMock 2.6.0 从 RC1 到 RC2 需要 2 年时间,然后又需要 2 年时间才真正发布。
关于代理和代理CGLIB 与仪器:
以上内容复制自 http://jmockit.org/about.html 。此外,它在几个方面对自身 (JMockit)、PowerMock 和 Mockito 进行了比较:
(当然,消息来源可能有偏见,但是……)
我会说使用JMockit。它是最容易使用、灵活的,并且适用于几乎所有情况,甚至是当您无法控制要测试的类(或者由于兼容性原因等而无法破坏它)时的困难情况和场景。
我使用 JMockit 的经历非常积极。
Update Sep 2019: The only mocking framework supported (by default) by Spring Boot is Mockito. If you use Spring, the answer is quite obvious.
I'd say the competition is between JMockit and PowerMock, then Mockito.
I'd leave "plain" jMock and EasyMock because they use only proxy & CGLIB and do not use Java 5 instrumentation like the newer frameworks.
jMock also didn't have a stable release for over 4 years. jMock 2.6.0 required 2 years to go from RC1 to RC2, and then another 2 years before it actually got released.
Regarding Proxy & CGLIB vs instrumentation:
The above is copied from http://jmockit.org/about.html . Further, it compares between itself (JMockit), PowerMock, and Mockito in several ways:
(granted, the source may be biased, but well...)
I'd say go with JMockit. It's the easiest to use, flexible, and works for pretty much all cases even difficult ones and scenarios when you can't control the class to be tested (or you can't break it due to compatibility reasons etc.).
My experiences with JMockit have been very positive.
我使用过 Mockito 和 JMockit,我对它们的体验是:
Mockito:
EasyMock:
JMockit:
此外,JMockit 的其他优点:
我个人更喜欢JMockit,我认为它的功能更丰富和灵活,但需要更陡峭的学习曲线。通常有多种方法可以实现相同的模拟效果,并且在设计模拟时需要更加小心。
I worked with both Mockito and JMockit, and my experience with them is:
Mockito:
EasyMock:
JMockit:
Besides, other benefits of JMockit:
I personally prefer JMockit, which I think is more feature rich and flexible, but requires a little bit steeper learning curve. There're usually multiple ways to achieve the same mocking effect, and requires more care when designing the mocks.
我仅使用 jMockit,因为它的反射库位于 Deencapsultation.class 中。我实际上很喜欢 Mockito 的风格,但我拒绝更改我的代码并弄乱我的 API,这样有限的测试框架就可以实现它。而且我喜欢测试我的所有代码,因此无法轻松测试私有方法的框架不是我想要使用的。
我被这篇文章所左右
经过(诚然,很大)的学习曲线之后,jMockit 现在是我的主要模拟单元测试框架。
I use jMockit only because of it's reflection libraries in Deencapsultation.class. I actually love Mockito's style, but I refuse to change my code and muddy up my API just so a limited testing framework can get at it. And I'm a fan of testing all my code, so a framework that can't easily test private methods is not what I want to be using.
I was swayed by this article
After a (admittedly large) learning curve, jMockit is now my main unit testing framework for mocks.
为了轻松测试我们的遗留代码库(包含大量静态方法调用等),JMockit 的价值无可估量。 [文章的无耻插件我的博客]
For easy testing of our legacy codebase (with lots of static method calls, etc.), JMockit has been invaluable. [Shameless plug for an article on my blog]
我个人更喜欢EasyMock。
在良好、正常和严格的模拟控制之间切换的能力是我最喜欢的功能之一。
I personally prefer EasyMock.
The ability to divert between nice, normal and strict mocking controls is one on my favorite feature.