問題描述
在 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 最好的模擬框架是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!