問(wèn)題描述
在 Java 中創(chuàng)建模擬對(duì)象的最佳框架是什么?為什么?每個(gè)框架的優(yōu)缺點(diǎn)是什么?
What's the best framework for creating mock objects in Java? Why? What are the pros and cons of each framework?
推薦答案
我在使用 Mockito一個(gè)>.
當(dāng)我嘗試學(xué)習(xí) JMock 和 EasyMock 時(shí),我發(fā)現(xiàn)學(xué)習(xí)曲線有點(diǎn)陡峭(盡管可能只是我自己).
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,因?yàn)樗恼Z(yǔ)法簡(jiǎn)單明了,我能夠很快掌握.最小語(yǔ)法旨在很好地支持常見(jiàn)情況,盡管有幾次我需要做一些更復(fù)雜的事情,但我發(fā)現(xiàn)我想要的東西得到了支持并且很容易掌握.
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.
這是來(lái)自 Mockito 主頁(yè)的(刪節(jié))示例:
Here's an (abridged) example from the Mockito homepage:
import static org.mockito.Mockito.*;
List mockedList = mock(List.class);
mockedList.clear();
verify(mockedList).clear();
沒(méi)有比這更簡(jiǎn)單的了.
我能想到的唯一主要缺點(diǎn)是它不會(huì)模擬靜態(tài)方法.
The only major downside I can think of is that it won't mock static methods.
這篇關(guān)于Java 最好的模擬框架是什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!