Home > DeveloperSection > Forums > What's the best mock framework for Java?
Jessica James
Jessica James

Total Post:16

Points:114
Posted on    July-23-2015 1:44 AM

 Java Java 
Ratings:


 1 Reply(s)
 407  View(s)
Rate this:
What's the best framework for creating mock objects in Java? Why? What are the pros and cons of each framework?


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    July-23-2015 7:50 AM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick