Mock frameworks for Java

After using EasyMock for several months I found Mockito. Yeah, the name sounds terrible in Spanish: little snot. It will be hard for me to tell spanish developers mockito features avoiding laughs. Guys, be careful with names next time 🙂

Mockito API is so cool that you no longer have to care if you're using a mock or a stub. You just use test doubles the way you need them and nothing more. Record/Replay metaphor is no longer used. The framework knows when are you writing the arrange, the act and the assert steps.

See how easy it is to mock out a void method (EmailAddress is a JPA Model):

  2. @Test
  3. public void persistorSaves() throws Exception {
  4. EmailAddress emailMock =
  5. mock(EmailAddress.class);
  6. Persistor<EmailAddress> persistor =
  7. new Persistor<EmailAddress>();
  9. persistor.Save(emailMock);
  11. verify(emailMock).save();
  12. }

Check out some examples:

My favorite mock frameworks so far are Mockito for Java and Rhino.Mocks for C#. They share pretty much all these nice features.

  • JVC

    Gracias por el tipo los voy a revisar!!!!


    Att. JVC 🙂

  • Tomek Kaczanowski

    Yeah, Mockito is currently the best choice for Java mocking.

    Still you better study the documentation carefully to avoid surprises. 🙂