我正在研究为我的项目使用哪个模拟框架,并将其范围缩小到 JMockit 和 Mockito.
I'm investigating which mocking framework to use for my project and have narrowed it down to JMockit and Mockito.
我注意到 Mockito 被选为the最好的 Java 模拟框架"在 Stackoverflow 上.
在比较 JMockit 的模拟工具的功能时比较矩阵",JMockit 似乎有多种不同的特征.
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.
对于 Mockito 可以做什么而 JMockit 无法实现的事情,是否有人有任何具体信息(不是意见),反之亦然?
Does anyone have any specific information (not opinions) on what Mockito can do which can't be achieved with JMockit and vice versa?
2019年9月更新:唯一的Spring Boot 支持的模拟框架(默认) 是 Mockito.如果你使用 Spring,答案就很明显了.
Update Sep 2019: The only mocking framework supported (by default) by Spring Boot is Mockito. If you use Spring, the answer is quite obvious.
我想说的是JMockit和PowerMock之间的竞争,然后是Mockito.
I'd say the competition is between JMockit and PowerMock, then Mockito.
我会留下普通"的 jMock 和 EasyMock,因为它们只使用代理和CGLIB,并且不要像新框架那样使用 Java 5 工具.
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 也有超过 4 年没有稳定的版本.jMock 2.6.0 从 RC1 升级到 RC2 需要 2 年,然后再需要 2 年才能真正发布.
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.
关于代理和CGLIB 与仪表:
Regarding Proxy & CGLIB vs instrumentation:
(EasyMock 和 jMock)基于 java.lang.reflect.Proxy,这需要一个接口实施的.此外,他们支持创建模拟对象对于通过 CGLIB 子类的类一代.正因为如此,说类不能是最终的,只有可覆盖的实例方法可以是嘲笑.然而,最重要的是,使用这些工具时被测代码的依赖项(即是,其他类的对象哪个给定的类正在测试取决于)必须由控制测试,以便模拟实例可以传递给那些客户依赖关系.因此,依赖不能简单地用客户端类中的新运算符我们要编写单元测试.
(EasyMock and jMock) are based on java.lang.reflect.Proxy, which requires an interface to be implemented. Additionally, they support the creation of mock objects for classes through CGLIB subclass generation. Because of that, said classes cannot be final and only overridable instance methods can be mocked. Most importantly, however, when using these tools the dependencies of code under test (that is, the objects of other classes on which a given class under test depends) must be controlled by the tests, so that mock instances can be passed to the clients of those dependencies. Therefore, dependencies can't simply be instantiated with the new operator in a client class for which we want to write unit tests.
最终,技术限制传统的模拟工具强加以下设计限制生产代码:
Ultimately, the technical limitations of conventional mocking tools impose the following design restrictions on production code:
以上内容复制自 http://jmockit.org/about.html .此外,它还通过多种方式在自身 (JMockit)、PowerMock 和 Mockito 之间进行比较:
The above is copied from http://jmockit.org/about.html . Further, it compares between itself (JMockit), PowerMock, and Mockito in several ways:
现在有其他模拟工具Java里面也克服了传统方法的局限,它们之间有 PowerMock、jEasyTest 和模拟注入.最接近的那个JMockit 的功能集是PowerMock,所以我将简要评估一下它在这里(此外,其他两个是更有限,似乎不是不再积极开发).
There are now other mocking tools for Java which also overcome the limitations of the conventional ones, between them PowerMock, jEasyTest, and MockInject. The one that comes closest to the feature set of JMockit is PowerMock, so I will briefly evaluate it here (besides, the other two are more limited and don't seem to be actively developed anymore).
另一个最近的模拟工具是莫基托.虽然没有尝试克服老年人的局限工具(jMock,EasyMock),它确实引入一种新的行为方式用模拟测试.JMockit 也支持这种另类风格,通过 Verifications API.
Another recent mocking tool is Mockito. Although it does not attempt to overcome the limitations of older tools (jMock, EasyMock), it does introduce a new style of behavior testing with mocks. JMockit also supports this alternative style, through the Verifications API.
最后,JMockit 测试工具包范围更广并且更有野心目标 比其他模拟工具包,在为了提供一个完整的和复杂的开发人员测试解决方案.一个很好的模拟 API,甚至没有人为的限制,不是足以生产性地创造测试.与 IDE 无关,易于使用,和集成良好的代码覆盖率工具也是必不可少的,这就是JMockit Coverage 旨在提供.开发人员测试的另一部分将变得更有用的工具集随着测试套件规模的扩大,增量重新运行测试的能力在对生产进行局部更改后代码;这也包含在覆盖工具.
Finally, the JMockit Testing Toolkit has a wider scope and more ambitious goals than other mocking toolkits, in order to provide a complete and sophisticated developer testing solution. A good API for mocking, even without artificial limitations, is not enough for productive creation of tests. An IDE-agnostic, easy to use, and well integrated Code Coverage tool is also essential, and that's what JMockit Coverage aims to provide. Another piece of the developer testing toolset which will become more useful as the test suite grows in size is the ability to incrementally rerun tests after a localized change to production code; this is also included in the Coverage tool.
(当然,来源可能有偏见,但好吧......)
(granted, the source may be biased, but well...)
我会说使用 JMockit.当您无法控制要测试的类(或由于兼容性等原因而无法破坏它)时,它最容易使用、灵活,并且适用于几乎所有情况,甚至是困难的情况和场景.
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.).
我对 JMockit 的体验非常积极.
My experiences with JMockit have been very positive.
这篇关于Mockito 与 JMockit 之间的比较 - 为什么 Mockito 的投票比 JMockit 好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!