245
110
|
What's the best framework for creating mock objects in Java? Why? What are the pros and cons of each framework?
| |||
closed as not constructive by agf, Bill 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.
196
|
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:
It doesn't get much simpler than that.
The only major downside I can think of is that it won't mock static methods.
| ||||||||||||||||||||
|
61
|
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.
| ||||||||||||||||||||
|