What#39;s the best mock framework for Java?(Java 最好的模拟框架是什么?)
问题描述
在 Java 中创建模拟对象的最佳框架是什么?为什么?每个框架的优缺点是什么?
What's the best framework for creating mock objects in Java? Why? What are the pros and cons of each framework?
推荐答案
我在使用 Mockito一个>.
当我尝试学习 JMock 和 EasyMock 时,我发现学习曲线有点陡峭(尽管可能只是我自己).
When I tried learning about JMock and EasyMock, I found the learning curve to be a bit steep (though maybe that's just me).
我喜欢 Mockito,因为它的语法简单明了,我能够很快掌握.最小语法旨在很好地支持常见情况,尽管有几次我需要做一些更复杂的事情,但我发现我想要的东西得到了支持并且很容易掌握.
I like Mockito because of its simple and clean syntax that I was able to grasp pretty quickly. The minimal syntax is designed to support the common cases very well, although the few times I needed to do something more complicated I found what I wanted was supported and easy to grasp.
这是来自 Mockito 主页的(删节)示例:
Here's an (abridged) example from the Mockito homepage:
import static org.mockito.Mockito.*;
List mockedList = mock(List.class);
mockedList.clear();
verify(mockedList).clear();
没有比这更简单的了.
我能想到的唯一主要缺点是它不会模拟静态方法.
The only major downside I can think of is that it won't mock static methods.
这篇关于Java 最好的模拟框架是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 最好的模拟框架是什么?


- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01