Sunday, October 27, 2013

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

closed as not constructive by agfBill the Lizard Oct 1 '11 at 12:19

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

15 Answers

up vote196down voteaccepted
I've had good success using Mockito.
When I tried learning about JMock and EasyMock, I found the learning curve to be a bit steep (though maybe that's just me).
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.
Here's an (abridged) example from the Mockito homepage:
import static org.mockito.Mockito.*;

List mockedList = mock(List.class);
mockedList.clear();
verify(mockedList).clear();
It doesn't get much simpler than that.
The only major downside I can think of is that it won't mock static methods.
share|improve this answer
7 
Beautiful. For static methods, simply combine Mockito with JMockit and there is practically no class too "legacy" for you to be able to test. –  Epaga Mar 13 '09 at 14:26
3 
I love how when you try to do something you shouldn't (e.g. create mocks inline), you get a very clear explanation of what you did wrong in the exception message. –  ripper234 Jun 27 '09 at 12:00
1 
The only think I don't like about mockito is that you cannot use it for android, other than that is a great framework –  MexicanHacker Mar 19 '10 at 18:28
2 
Is the conclusion still the same as of March 2011? –  Thorbjørn Ravn Andersen Mar 1 '11 at 13:59
1 
For me, absolutely. I would still recommend it unconditionally. Of course, if you find another framework that better meets your needs, mention it in another answer and see what votes it gets and what kinds of comments it receives. –  Brian Laframboise Mar 2 '11 at 15:24
show 9 more comments
I am the creator of PowerMock so obviously I must recommend that! :-)
PowerMock extends both EasyMock and Mockito with the ability to mock static methods, final and even private methods. The EasyMock support is complete, but the Mockito plugin needs some more work. We are planning to add JMock support as well.
PowerMock is not intended to replace other frameworks, rather it can be used in the tricky situations when other frameworks does't allow mocking. PowerMock also contains other useful features such assuppressing static initializers and constructors.
share|improve this answer
1 
Looking good, I'll give it a try :) –  Peter Perháč May 9 '09 at 18:57
 
Powermock is essential for unit testing Android applications using native Java on the host PC (avoiding using the slow emulator) –  Jeff Axelrod Jul 14 '11 at 18:08
 
@Jan only issue is PowerMock is incompatible when using Robolectric though :-( I want to do @RunWith(PowerMockRunner.class) AND @RunWith(RobolectricTestRunner.class) –  Blundell Oct 22 '11 at 0:29 
 
After using PowerMock for the past few months I heartily recommend it! –  cwash Jan 3 '12 at 19:59
 
PowerMock really is awesome. I really love static singletons to access everything, and this makes that possible to test. –  simplyianm Mar 17 '12 at 8:42

No comments: