Confused when implementing testing

1

I want to implement continuous integration in my project but I have some doubts, suppose we have this:

public boolean sumar(Persona person) {
    int result = 0;
    List<Personas> personas = null;

    try {           
        jdbcService.save(Personas.class, person);
        personas = jdbcService.list(Personas.class);
        result = utilsService.sum(personas);
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return result;
}
  • How would you do to try the 3 methods with Mock
  • What is the difference between test unit and integration?
asked by sirdaiz 05.05.2017 в 13:27
source

2 answers

0

Simplifying a lot very much, the unitaries are to test the functionality and the integration functionality within an environment (with other systems such as a database).

As for what questions of how to test, I can guide you on how to do it with Mockito. It's pretty easy, for example, you would have to do something like:

UtilService util = Mockito.mock(UtilService.class);
[Mi objeto con el método suma].setUtilSerive(util); (o en el constructor)

and invoke [My object with the sum method] .sum (xxx);

To modify the behavior of the mock you can use when . Something like this:

Mockito.when(util.sum(20)).thenReturn(xxxx);

basically, what it does is you tell it that when you invoke the method with the value 20, return XXXX (whatever).

If you want to expand more information about mocks with Mockito, I recommend that you go through the mockito page: link

I hope it serves you

    
answered by 05.05.2017 в 14:17
0

First, three methods are not tested here. One is tested ( sumar ).

Unit tests are made from the code. I do not know the dependencies of jdbcService.save or utilsService.sum , so I can not write unit tests.

And it would be something like that

 JdbcService jdbcService =
    mock(JdbcService.class);
 UtilsService utilsService =
    mock(UtilsService.class);

 List<Persona> lista =
   new ArrayList<>();
 when(
   jdbcService.list()).
   thenReturn(lista);
 doReturn(69).
   when(utilsService).
     sum(eq(lista));

 ... Crear instancia del objeto, inyectar mocks

 assertEquals(
    "El resultado no coincide",
    69,
    resultadoDeSumar);
 ... y si acaso...
 verify(
    jdbcService, times(1)).
    save(
      eq(Personas.class),
      eq(miObjetoPersonaQuePasePorParámetroASumar));

The integration tests are done without substituting the modules by dependencies, so you check that there are no errors caused by discrepancies between what the dependencies are supposed to do and what they really do. But, naturally, when dealing with more classes, they are much more complex to implement.

    
answered by 05.05.2017 в 14:18