What's the best mock framework for Java?

JavaUnit TestingMocking

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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 as suppressing static initializers and constructors.

Solution 3 - Java

The http://jmockit.org">JMockit project site contains plenty of comparative information for current mocking toolkits.

In particular, check out the http://jmockit.org/MockingToolkitComparisonMatrix.html">feature comparison matrix, which covers EasyMock, jMock, Mockito, Unitils Mock, PowerMock, and of course JMockit. I try to keep it accurate and up-to-date, as much as possible.

Solution 4 - Java

I've been having success with JMockit.

It's pretty new, and so it's a bit raw and under-documented. It uses ASM to dynamically redefine the class bytecode, so it can mock out all methods including static, private, constructors, and static initializers. For example:

import mockit.Mockit;

...
Mockit.redefineMethods(MyClassWithStaticInit.class,
                       MyReplacementClass.class);
...
class MyReplacementClass {
  public void $init() {...} // replace default constructor
  public static void $clinit{...} // replace static initializer
  public static void myStatic{...} // replace static method
  // etc...
}

It has an Expectations interface allowing record/playback scenarios as well:

import mockit.Expectations;
import org.testng.annotations.Test;

public class ExpecationsTest {
  private MyClass obj;

  @Test
  public void testFoo() {
    new Expectations(true) {
      MyClass c;
      {
        obj = c;
        invokeReturning(c.getFoo("foo", false), "bas");
      }
    };

    assert "bas".equals(obj.getFoo("foo", false));

    Expectations.assertSatisfied();
  }

  public static class MyClass {
    public String getFoo(String str, boolean bool) {
      if (bool) {
        return "foo";
      } else {
        return "bar";
      }
    }
  }
}

The downside is that it requires Java 5/6.

Solution 5 - Java

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

Solution 6 - Java

I started using mocks with EasyMock. Easy enough to understand, but the replay step was kinda annoying. [Mockito][2] 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][3] 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][2] with [PowerMock][4] 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.

[2]: http://code.google.com/p/mockito/ "Mockito" [3]: http://jmockit.org/ "JMockit" [4]: http://code.google.com/p/powermock/

Solution 7 - Java

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.

Solution 8 - Java

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.

Solution 9 - Java

I like JMock because you are able to set up expectations. This is totally different from checking if a method was called found in some mock libraries. Using JMock you can write very sophisticated expectations. See the jmock cheat-sheat.

Solution 10 - Java

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

Solution 11 - Java

The best solution to mocking is to have the machine do all the work with automated specification-based testing. For Java, see ScalaCheck and the Reductio framework included in the Functional Java library. With automated specification-based testing frameworks, you supply a specification of the method under test (a property about it that should be true) and the framework generates tests as well as mock objects, automatically.

For example, the following property tests the Math.sqrt method to see if the square root of any positive number n squared is equal to n.

val propSqrt = forAll { (n: Int) => (n >= 0) ==> scala.Math.sqrt(n*n) == n }

When you call propSqrt.check(), ScalaCheck generates hundreds of integers and checks your property for each, also automatically making sure that the edge cases are covered well.

Even though ScalaCheck is written in Scala, and requires the Scala Compiler, it's easy to test Java code with it. The Reductio framework in Functional Java is a pure Java implementation of the same concepts.

Solution 12 - Java

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.

Solution 13 - Java

For something a little different, you could use JRuby and Mocha which are combined in JtestR to write tests for your Java code in expressive and succinct Ruby. There are some useful mocking examples with JtestR here. One advantage of this approach is that mocking concrete classes is very straightforward.

Solution 14 - Java

I started using mocks through JMock, but eventually transitioned to use EasyMock. EasyMock was just that, --easier-- and provided a syntax that felt more natural. I haven't switched since.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJosh BrownView Question on Stackoverflow
Solution 1 - JavaBrian LaframboiseView Answer on Stackoverflow
Solution 2 - JavaJan KronquistView Answer on Stackoverflow
Solution 3 - JavaRogérioView Answer on Stackoverflow
Solution 4 - JavaKris PrudenView Answer on Stackoverflow
Solution 5 - Javap3t0rView Answer on Stackoverflow
Solution 6 - JavatrafalmadorianView Answer on Stackoverflow
Solution 7 - JavadlinsinView Answer on Stackoverflow
Solution 8 - JavaDmitryView Answer on Stackoverflow
Solution 9 - JavaAndrea FranciaView Answer on Stackoverflow
Solution 10 - JavaBartosz BierkowskiView Answer on Stackoverflow
Solution 11 - JavaApocalispView Answer on Stackoverflow
Solution 12 - JavaJosh BrownView Answer on Stackoverflow
Solution 13 - JavaJames MeadView Answer on Stackoverflow
Solution 14 - JavaMike FurtakView Answer on Stackoverflow