Mockito vs EasyMock vs JMockit

1. Introducción

1.1. Visión general

En esta publicación, vamos a hablar sobre el simulacro : qué es, por qué usarlo y varios ejemplos de cómo simular el mismo caso de prueba usando algunas de las bibliotecas de simulacros más utilizadas para Java.

Comenzaremos con algunas definiciones formales / semiformales de conceptos burlones; luego presentaremos el caso bajo prueba, seguiremos con ejemplos para cada biblioteca y terminaremos con algunas conclusiones. Las bibliotecas elegidas son Mockito, EasyMock y JMockit.

Si siente que ya conoce los conceptos básicos de la burla, tal vez pueda pasar al punto 2 sin leer los siguientes tres puntos.

1.2. Razones para usar simulacros

Comenzaremos asumiendo que ya codificas siguiendo alguna metodología de desarrollo dirigida centrada en pruebas (TDD, ATDD o BDD). O simplemente que desea crear una prueba para una clase existente que se basa en dependencias para lograr su funcionalidad.

En cualquier caso, cuando hacemos pruebas unitarias de una clase, queremos probar solo su funcionalidad y no la de sus dependencias (ya sea porque confiamos en su implementación o porque la probaremos nosotros mismos).

Para lograr esto, necesitamos proporcionar al objeto bajo prueba, un reemplazo que podamos controlar para esa dependencia. De esta manera podemos forzar valores de retorno extremos, lanzar excepciones o simplemente reducir los métodos que consumen mucho tiempo a un valor de retorno fijo.

Este reemplazo controlado es un simulacro y le ayudará a simplificar la codificación de la prueba y a reducir el tiempo de ejecución de la prueba.

1.3. Simulación de conceptos y definición

Veamos cuatro definiciones de un artículo escrito por Martin Fowler que resume los conceptos básicos que todos deberían saber sobre las simulaciones:

  • Los objetos ficticios se pasan de un lado a otro, pero nunca se utilizan. Por lo general, solo se utilizan para completar listas de parámetros.
  • Los objetos falsos tienen implementaciones que funcionan, pero por lo general, toman algún atajo que los hace no aptos para la producción (una base de datos en memoria es un buen ejemplo).
  • Los stubs brindan respuestas almacenadas a las llamadas realizadas durante la prueba, por lo general, no responden en absoluto a nada fuera de lo programado para la prueba. Los talones también pueden registrar información sobre las llamadas, como un talón de la puerta de enlace de correo electrónico que recuerda los mensajes que 'envió', o quizás solo la cantidad de mensajes que 'envió'.
  • Las simulaciones son de lo que estamos hablando aquí: objetos preprogramados con expectativas que forman una especificación de las llamadas que se espera que reciban.

1.4 Burlarse o no burlarse: esa es la cuestión

No todo debe ser burlado . A veces es mejor hacer una prueba de integración, ya que burlarse de ese método / característica solo funcionaría para obtener pocos beneficios reales. En nuestro caso de prueba (que se mostrará en el siguiente punto) eso sería probar el LoginDao .

El LoginDao usaría alguna biblioteca de terceros para el acceso a la base de datos, y burlarse de ella solo consistiría en asegurar que los parámetros se hayan preparado para la llamada, pero aún así necesitaríamos probar que la llamada devuelve los datos que queremos.

Por esa razón, no se incluirá en este ejemplo (aunque podríamos escribir tanto la prueba unitaria con llamadas simuladas para las llamadas a la biblioteca de terceros Y una prueba de integración con DBUnit para probar el rendimiento real de la biblioteca de terceros).

2. Caso de prueba

Con todo lo de la sección anterior en mente, propongamos un caso de prueba bastante típico y cómo lo probaremos usando simulacros (cuando tiene sentido usar simulacros). Esto nos ayudará a tener un escenario común para luego poder comparar las diferentes librerías simuladas.

2.1 Caso propuesto

El caso de prueba propuesto será el proceso de inicio de sesión en una aplicación con una arquitectura en capas.

La solicitud de inicio de sesión será manejada por un controlador, que usa un servicio, que usa un DAO (que busca las credenciales de usuario en una base de datos). No profundizaremos demasiado en la implementación de cada capa y nos centraremos más en las interacciones entre los componentes de cada capa.

De esta manera, tendremos un LoginController , un LoginService y un LoginDAO . Veamos un diagrama para aclarar:

2.2 Implementación

Seguiremos ahora con la implementación utilizada para el caso de prueba, para que podamos entender qué está pasando (o qué debería pasar) en las pruebas.

Comenzaremos con el modelo utilizado para todas las operaciones, UserForm , que solo contendrá el nombre y la contraseña del usuario (estamos usando modificadores de acceso público para simplificar) y un método getter para el campo de nombre de usuario para permitir burlarse de esa propiedad:

public class UserForm { public String password; public String username; public String getUsername(){ return username; } }

Sigamos con LoginDAO , que carecerá de funcionalidad ya que solo queremos que sus métodos estén allí para poder burlarnos de ellos cuando sea necesario:

public class LoginDao { public int login(UserForm userForm){ return 0; } }

LoginDao será utilizado por LoginService en su método de inicio de sesión . LoginService también tendrá un método setCurrentUser que devuelve void para probar esa burla.

public class LoginService { private LoginDao loginDao; private String currentUser; public boolean login(UserForm userForm) { assert null != userForm; int loginResults = loginDao.login(userForm); switch (loginResults){ case 1: return true; default: return false; } } public void setCurrentUser(String username) { if(null != username){ this.currentUser = username; } } }

Finalmente, LoginController utilizará LoginService para su método de inicio de sesión . Esto incluirá:

  • un caso en el que no se realizarán llamadas al servicio simulado.
  • un caso en el que solo se llamará a un método.
  • un caso en el que se llamarán todos los métodos.
  • un caso en el que se probará el lanzamiento de excepciones.
public class LoginController { public LoginService loginService; public String login(UserForm userForm){ if(null == userForm){ return "ERROR"; }else{ boolean logged; try { logged = loginService.login(userForm); } catch (Exception e) { return "ERROR"; } if(logged){ loginService.setCurrentUser(userForm.getUsername()); return "OK"; }else{ return "KO"; } } } }

Ahora que hemos visto qué es lo que estamos tratando de probar, veamos cómo lo simularemos con cada biblioteca.

3. Configuración de prueba

3.1 Mockito

Para Mockito usaremos la versión 2.8.9.

La forma más fácil de crear y usar simulacros es a través de las anotaciones @Mock y @InjectMocks . El primero creará un simulacro para la clase utilizada para definir el campo y el segundo intentará inyectar dichos simulacros creados en el simulacro anotado.

There are more annotations such as @Spy that lets you create a partial mock (a mock that uses the normal implementation in non-mocked methods).

That being said, you need to call MockitoAnnotations.initMocks(this) before executing any tests that would use said mocks for all of this “magic” to work. This is usually done in a @Before annotated method. You can also use the MockitoJUnitRunner.

public class LoginControllerTest { @Mock private LoginDao loginDao; @Spy @InjectMocks private LoginService spiedLoginService; @Mock private LoginService loginService; @InjectMocks private LoginController loginController; @Before public void setUp() { loginController = new LoginController(); MockitoAnnotations.initMocks(this); } }

3.2 EasyMock

For EasyMock, we'll be using version 3.4 (Javadoc). Note that with EasyMock, for mocks to start “working”, you must call EasyMock.replay(mock) on every test method, or you will receive an exception.

Mocks and tested classes can also be defined via annotations, but in this case, instead of calling a static method for it to work, we'll be using the EasyMockRunner for the test class.

Mocks are created with the @Mock annotation and the tested object with the @TestSubject one (which will get its dependencies injected from created mocks). The tested object must be created in-line.

@RunWith(EasyMockRunner.class) public class LoginControllerTest { @Mock private LoginDao loginDao; @Mock private LoginService loginService; @TestSubject private LoginController loginController = new LoginController(); }

3.3. JMockit

For JMockit we'll be using version 1.24 (Javadoc) as version 1.25 hasn't been released yet (at least while writing this).

Setup for JMockit is as easy as with Mockito, with the exception that there is no specific annotation for partial mocks (and really no need either) and that you must use JMockit as the test runner.

Mocks are defined using the @Injectable annotation (that will create only one mock instance) or with @Mocked annotation (that will create mocks for every instance of the class of the annotated field).

The tested instance gets created (and its mocked dependencies injected) using the @Tested annotation.

@RunWith(JMockit.class) public class LoginControllerTest { @Injectable private LoginDao loginDao; @Injectable private LoginService loginService; @Tested private LoginController loginController; }

4. Verifying No Calls to Mock

4.1. Mockito

For verifying that a mock received no calls in Mockito, you have the method verifyZeroInteractions() that accepts a mock.

@Test public void assertThatNoMethodHasBeenCalled() { loginController.login(null); Mockito.verifyZeroInteractions(loginService); }

4.2. EasyMock

For verifying that a mock received no calls you simply don't specify behavior, you replay the mock, and lastly, you verify it.

@Test public void assertThatNoMethodHasBeenCalled() { EasyMock.replay(loginService); loginController.login(null); EasyMock.verify(loginService); }

4.3. JMockit

For verifying that a mock received no calls you simply don't specify expectations for that mock and do a FullVerifications(mock) for said mock.

@Test public void assertThatNoMethodHasBeenCalled() { loginController.login(null); new FullVerifications(loginService) {}; }

5. Defining Mocked Method Calls and Verifying Calls to Mocks

5.1. Mockito

For mocking method calls, you can use Mockito.when(mock.method(args)).thenReturn(value). Here you can return different values for more than one call just adding them as more parameters: thenReturn(value1, value2, value-n, …).

Note that you can't mock void returning methods with this syntax. In said cases, you'll use a verification of said method (as shown on line 11).

For verifying calls to a mock you can use Mockito.verify(mock).method(args) and you can also verify that no more calls were done to a mock using verifyNoMoreInteractions(mock).

For verifying args, you can pass specific values or use predefined matchers like any(), anyString(), anyInt(). There are a lot more of that kind of matchers and even the possibility to define your matchers which we'll see in following examples.

@Test public void assertTwoMethodsHaveBeenCalled() { UserForm userForm = new UserForm(); userForm.username = "foo"; Mockito.when(loginService.login(userForm)).thenReturn(true); String login = loginController.login(userForm); Assert.assertEquals("OK", login); Mockito.verify(loginService).login(userForm); Mockito.verify(loginService).setCurrentUser("foo"); } @Test public void assertOnlyOneMethodHasBeenCalled() { UserForm userForm = new UserForm(); userForm.username = "foo"; Mockito.when(loginService.login(userForm)).thenReturn(false); String login = loginController.login(userForm); Assert.assertEquals("KO", login); Mockito.verify(loginService).login(userForm); Mockito.verifyNoMoreInteractions(loginService); }

5.2. EasyMock

For mocking method calls, you use EasyMock.expect(mock.method(args)).andReturn(value).

For verifying calls to a mock, you can use EasyMock.verify(mock), but you must call it always after calling EasyMock.replay(mock).

For verifying args, you can pass specific values, or you have predefined matchers like isA(Class.class), anyString(), anyInt(), and a lot more of that kind of matchers and again the possibility to define your matchers.

@Test public void assertTwoMethodsHaveBeenCalled() { UserForm userForm = new UserForm(); userForm.username = "foo"; EasyMock.expect(loginService.login(userForm)).andReturn(true); loginService.setCurrentUser("foo"); EasyMock.replay(loginService); String login = loginController.login(userForm); Assert.assertEquals("OK", login); EasyMock.verify(loginService); } @Test public void assertOnlyOneMethodHasBeenCalled() { UserForm userForm = new UserForm(); userForm.username = "foo"; EasyMock.expect(loginService.login(userForm)).andReturn(false); EasyMock.replay(loginService); String login = loginController.login(userForm); Assert.assertEquals("KO", login); EasyMock.verify(loginService); }

5.3. JMockit

With JMockit, you have defined steps for testing: record, replay and verify.

Record is done in a new Expectations(){{}} block (into which you can define actions for several mocks), replay is done simply by invoking a method of the tested class (that should call some mocked object), and verification is done inside a new Verifications(){{}} block (into which you can define verifications for several mocks).

For mocking method calls, you can use mock.method(args); result = value; inside any Expectations block. Here you can return different values for more than one call just using returns(value1, value2, …, valuen); instead of result = value;.

For verifying calls to a mock you can use new Verifications(){{mock.call(value)}} or new Verifications(mock){{}} to verify every expected call previously defined.

For verifying args, you can pass specific values, or you have predefined values like any, anyString, anyLong, and a lot more of that kind of special values and again the possibility to define your matchers (that must be Hamcrest matchers).

@Test public void assertTwoMethodsHaveBeenCalled() { UserForm userForm = new UserForm(); userForm.username = "foo"; new Expectations() {{ loginService.login(userForm); result = true; loginService.setCurrentUser("foo"); }}; String login = loginController.login(userForm); Assert.assertEquals("OK", login); new FullVerifications(loginService) {}; } @Test public void assertOnlyOneMethodHasBeenCalled() { UserForm userForm = new UserForm(); userForm.username = "foo"; new Expectations() {{ loginService.login(userForm); result = false; // no expectation for setCurrentUser }}; String login = loginController.login(userForm); Assert.assertEquals("KO", login); new FullVerifications(loginService) {}; }

6. Mocking Exception Throwing

6.1. Mockito

Exception throwing can be mocked using .thenThrow(ExceptionClass.class) after a Mockito.when(mock.method(args)).

@Test public void mockExceptionThrowin() { UserForm userForm = new UserForm(); Mockito.when(loginService.login(userForm)).thenThrow(IllegalArgumentException.class); String login = loginController.login(userForm); Assert.assertEquals("ERROR", login); Mockito.verify(loginService).login(userForm); Mockito.verifyZeroInteractions(loginService); }

6.2. EasyMock

Exception throwing can be mocked using .andThrow(new ExceptionClass()) after an EasyMock.expect(…) call.

@Test public void mockExceptionThrowing() { UserForm userForm = new UserForm(); EasyMock.expect(loginService.login(userForm)).andThrow(new IllegalArgumentException()); EasyMock.replay(loginService); String login = loginController.login(userForm); Assert.assertEquals("ERROR", login); EasyMock.verify(loginService); }

6.3. JMockit

Mocking exception throwing with JMockito is especially easy. Just return an Exception as the result of a mocked method call instead of the “normal” return.

@Test public void mockExceptionThrowing() { UserForm userForm = new UserForm(); new Expectations() {{ loginService.login(userForm); result = new IllegalArgumentException(); // no expectation for setCurrentUser }}; String login = loginController.login(userForm); Assert.assertEquals("ERROR", login); new FullVerifications(loginService) {}; }

7. Mocking an Object to Pass Around

7.1. Mockito

You can create a mock also to pass as an argument for a method call. With Mockito, you can do that with a one-liner.

@Test public void mockAnObjectToPassAround() { UserForm userForm = Mockito.when(Mockito.mock(UserForm.class).getUsername()) .thenReturn("foo").getMock(); Mockito.when(loginService.login(userForm)).thenReturn(true); String login = loginController.login(userForm); Assert.assertEquals("OK", login); Mockito.verify(loginService).login(userForm); Mockito.verify(loginService).setCurrentUser("foo"); }

7.2. EasyMock

Mocks can be created in-line with EasyMock.mock(Class.class). Afterward, you can use EasyMock.expect(mock.method()) to prepare it for execution, always remembering to call EasyMock.replay(mock) before using it.

@Test public void mockAnObjectToPassAround() { UserForm userForm = EasyMock.mock(UserForm.class); EasyMock.expect(userForm.getUsername()).andReturn("foo"); EasyMock.expect(loginService.login(userForm)).andReturn(true); loginService.setCurrentUser("foo"); EasyMock.replay(userForm); EasyMock.replay(loginService); String login = loginController.login(userForm); Assert.assertEquals("OK", login); EasyMock.verify(userForm); EasyMock.verify(loginService); }

7.3. JMockit

To mock an object for just one method, you can simply pass it mocked as a parameter to the test method. Then you can create expectations as with any other mock.

@Test public void mockAnObjectToPassAround(@Mocked UserForm userForm) { new Expectations() {{ userForm.getUsername(); result = "foo"; loginService.login(userForm); result = true; loginService.setCurrentUser("foo"); }}; String login = loginController.login(userForm); Assert.assertEquals("OK", login); new FullVerifications(loginService) {}; new FullVerifications(userForm) {}; }

8. Custom Argument Matching

8.1. Mockito

Sometimes argument matching for mocked calls needs to be a little more complex than just a fixed value or anyString(). For that cases with Mockito has its matcher class that is used with argThat(ArgumentMatcher).

@Test public void argumentMatching() { UserForm userForm = new UserForm(); userForm.username = "foo"; // default matcher Mockito.when(loginService.login(Mockito.any(UserForm.class))).thenReturn(true); String login = loginController.login(userForm); Assert.assertEquals("OK", login); Mockito.verify(loginService).login(userForm); // complex matcher Mockito.verify(loginService).setCurrentUser(ArgumentMatchers.argThat( new ArgumentMatcher() { @Override public boolean matches(String argument) { return argument.startsWith("foo"); } } )); }

8.2. EasyMock

Custom argument matching is a little bit more complicated with EasyMock as you need to create a static method in which you create the actual matcher and then report it with EasyMock.reportMatcher(IArgumentMatcher).

Once this method is created, you use it on your mock expectation with a call to the method (like seen in the example in line ).

@Test public void argumentMatching() { UserForm userForm = new UserForm(); userForm.username = "foo"; // default matcher EasyMock.expect(loginService.login(EasyMock.isA(UserForm.class))).andReturn(true); // complex matcher loginService.setCurrentUser(specificArgumentMatching("foo")); EasyMock.replay(loginService); String login = loginController.login(userForm); Assert.assertEquals("OK", login); EasyMock.verify(loginService); } private static String specificArgumentMatching(String expected) { EasyMock.reportMatcher(new IArgumentMatcher() { @Override public boolean matches(Object argument) { return argument instanceof String && ((String) argument).startsWith(expected); } @Override public void appendTo(StringBuffer buffer) { //NOOP } }); return null; }

8.3. JMockit

Custom argument matching with JMockit is done with the special withArgThat(Matcher) method (that receives Hamcrest‘s Matcher objects).

@Test public void argumentMatching() { UserForm userForm = new UserForm(); userForm.username = "foo"; // default matcher new Expectations() {{ loginService.login((UserForm) any); result = true; // complex matcher loginService.setCurrentUser(withArgThat(new BaseMatcher() { @Override public boolean matches(Object item) { return item instanceof String && ((String) item).startsWith("foo"); } @Override public void describeTo(Description description) { //NOOP } })); }}; String login = loginController.login(userForm); Assert.assertEquals("OK", login); new FullVerifications(loginService) {}; }

9. Partial Mocking

9.1. Mockito

Mockito allows partial mocking (a mock that uses the real implementation instead of mocked method calls in some of its methods) in two ways.

You can either use .thenCallRealMethod() in a normal mock method call definition, or you can create a spy instead of a mock in which case the default behavior for that will be to call the real implementation in all non-mocked methods.

@Test public void partialMocking() { // use partial mock loginController.loginService = spiedLoginService; UserForm userForm = new UserForm(); userForm.username = "foo"; // let service's login use implementation so let's mock DAO call Mockito.when(loginDao.login(userForm)).thenReturn(1); String login = loginController.login(userForm); Assert.assertEquals("OK", login); // verify mocked call Mockito.verify(spiedLoginService).setCurrentUser("foo"); }

9.2. EasyMock

Partial mocking also gets a little more complicated with EasyMock, as you need to define which methods will be mocked when creating the mock.

This is done with EasyMock.partialMockBuilder(Class.class).addMockedMethod(“methodName”).createMock(). Once this is done, you can use the mock as any other non-partial mock.

@Test public void partialMocking() { UserForm userForm = new UserForm(); userForm.username = "foo"; // use partial mock LoginService loginServicePartial = EasyMock.partialMockBuilder(LoginService.class) .addMockedMethod("setCurrentUser").createMock(); loginServicePartial.setCurrentUser("foo"); // let service's login use implementation so let's mock DAO call EasyMock.expect(loginDao.login(userForm)).andReturn(1); loginServicePartial.setLoginDao(loginDao); loginController.loginService = loginServicePartial; EasyMock.replay(loginDao); EasyMock.replay(loginServicePartial); String login = loginController.login(userForm); Assert.assertEquals("OK", login); // verify mocked call EasyMock.verify(loginServicePartial); EasyMock.verify(loginDao); }

9.3. JMockit

Partial mocking with JMockit is especially easy. Every method call for which no mocked behavior has been defined in an Expectations(){{}} uses the “real” implementation.

Now let's imagine that we want to partially mock the LoginService class to mock the setCurrentUser() method while using the actual implementation of the login() method.

To do this, we first create and pass an instance of LoginService to the expectations block. Then, we only record an expectation for the setCurrentUser() method:

@Test public void partialMocking() { LoginService partialLoginService = new LoginService(); partialLoginService.setLoginDao(loginDao); loginController.loginService = partialLoginService; UserForm userForm = new UserForm(); userForm.username = "foo"; new Expectations(partialLoginService) {{ // let's mock DAO call loginDao.login(userForm); result = 1; // no expectation for login method so that real implementation is used // mock setCurrentUser call partialLoginService.setCurrentUser("foo"); }}; String login = loginController.login(userForm); Assert.assertEquals("OK", login); // verify mocked call new Verifications() {{ partialLoginService.setCurrentUser("foo"); }}; }

10. Conclusion

In this post, we've been comparing three Java mock libraries, each one with its strong points and downsides.

  • All three of them are easily configured with annotations to help you define mocks and the object-under-test, with runners to make mock injection as painless as possible.
    • We'd say Mockito would win here as it has a special annotation for partial mocks, but JMockit doesn't even need it, so let's say that it's a tie between those two.
  • All three of them follow more or less the record-replay-verify pattern, but in our opinion, the best one to do so is JMockit as it forces you to use those in blocks, so tests get more structured.
  • Easiness of use is important so you can work as less as possible to define your tests. JMockit will be the chosen option for its fixed-always-the-same structure.
  • Mockito is more or less THE most known so that the community will be bigger.
  • Having to call replay every time you want to use a mock is a clear no-go, so we'll put a minus one for EasyMock.
  • La coherencia / simplicidad también es importante para mí. Nos encantó la forma de devolver los resultados de JMockit, que es la misma para los resultados "normales" que para las excepciones.

Dicho todo esto, elegiremos a JMockit como una especie de ganador a pesar de que hasta ahora hemos estado usando Mockito ya que nos ha cautivado su simplicidad y estructura fija e intentaremos usarlo a partir de ahora. en.

La implementación completa de este tutorial se puede encontrar en el proyecto GitHub, así que no dudes en descargarlo y jugar con él.