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):

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

Check out some examples: http://schuchert.wikispaces.com/Mockito.LoginServiceExample

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

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • JVC

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

    Saludos!!!!

    Att. JVC 🙂

  • http://practicalunittesting.com Tomek Kaczanowski

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

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