What's the best mock framework for Java?

Josh Brown picture Josh Brown · Aug 22, 2008 · Viewed 209.9k times · Source

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

Answer

Brian Laframboise picture Brian Laframboise · Aug 22, 2008

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.