What's the best mock framework for Java? [clos

2019-01-01 06:22发布

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

14条回答
若你有天会懂
2楼-- · 2019-01-01 06:40

I used JMock early. I've tried Mockito at my last project and liked it. More concise, more cleaner. PowerMock covers all needs which are absent in Mockito, such as mocking a static code, mocking an instance creation, mocking final classes and methods. So I have all I need to perform my work.

查看更多
琉璃瓶的回忆
3楼-- · 2019-01-01 06:41

You could also have a look at testing using Groovy. In Groovy you can easily mock Java interfaces using the 'as' operator:

def request = [isUserInRole: { roleName -> roleName == "testRole"}] as HttpServletRequest 

Apart from this basic functionality Groovy offers a lot more on the mocking front, including the powerful MockFor and StubFor classes.

http://docs.codehaus.org/display/GROOVY/Groovy+Mocks

查看更多
美炸的是我
4楼-- · 2019-01-01 06:42

I started using mocks with EasyMock. Easy enough to understand, but the replay step was kinda annoying. Mockito removes this, also has a cleaner syntax as it looks like readability was one of its primary goals. I cannot stress enough how important this is, since most of developers will spend their time reading and maintaining existing code, not creating it.

Another nice thing is that interfaces and implementation classes are handled in the same way, unlike in EasyMock where still you need to remember (and check) to use an EasyMock Class Extension.

I've taken a quick look at JMockit recently, and while the laundry list of features is pretty comprehensive, I think the price of this is legibility of resulting code, and having to write more.

For me, Mockito hits the sweet spot, being easy to write and read, and dealing with majority of the situations most code will require. Using Mockito with PowerMock would be my choice.

One thing to consider is that the tool you would choose if you were developing by yourself, or in a small tight-knit team, might not be the best to get for a large company with developers of varying skill levels. Readability, ease of use and simplicity would need more consideration in the latter case. No sense in getting the ultimate mocking framework if a lot of people end up not using it or not maintaining the tests.

查看更多
何处买醉
5楼-- · 2019-01-01 06:43

Mockito also provides the option of stubbing methods, matching arguments (like anyInt() and anyString()), verifying the number of invocations (times(3), atLeastOnce(), never()), and more.

I've also found that Mockito is simple and clean.

One thing I don't like about Mockito is that you can't stub static methods.

查看更多
旧人旧事旧时光
6楼-- · 2019-01-01 06:44

We are heavily using EasyMock and EasyMock Class Extension at work and are pretty happy with it. It basically gives you everything you need. Take a look at the documentation, there's a very nice example which shows you all the features of EasyMock.

查看更多
栀子花@的思念
7楼-- · 2019-01-01 06:44

Yes, Mockito is a great framework. I use it together with hamcrest and Google guice to setup my tests.

查看更多
登录 后发表回答