本文介绍了什么是最好的Java模拟框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中创建模拟对象的最佳框架是什么?为什么?每个框架的优点和缺点是什么?

What's the best framework for creating mock objects in Java? Why? What are the pros and cons of each framework?

推荐答案

我使用。

当我尝试学习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模拟框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:32
查看更多