How to change the value of a mock at run time?

1

I have the following method:

public void ingresarCarga() {

    Integer idCarga = ingresoDao.ingresar();
    ResultadoIngreso resultado;

    do {
        resultado = archivoDao.procesar();
        try {
            if(resultado.getCodigo() == 1){
                Thread.sleep(10000);
            }
        } catch (InterruptedException e) {
            LOGGER.info("InterruptedException");
        } 
    }while (resultado.getCodigo() == 1);
}

Clearly you can notice that you can enter an infinite loop, but the code is not wrong. When trying to do the coverage tests I find the problem trying to cover the case when the answer is -60.

I have tried in different ways and none has worked for me:

    @Test(timeout=11000, expected = Exception.class)
    public void testIngreso2() throws Exception {
        Integer retorno = 1;

        Mockito.when(archivoDao.procesar()).thenReturn(resultado);
        Mockito.when(resultado.getCodigo()).thenReturn(retorno);

        instancia.ingresarCarga();
}

In this case the test fails and returns the following error:

java.lang.Exception: test timed out after 11000 milliseconds     at java.lang.Thread.sleep (Native Method)

I also thought of a way to change the value of the return in the second iteration but so far I could not get it.

I'm working with java 7 and junit 4.11

Thank you very much for the help in advance: D.

    
asked by Williams197 29.05.2018 в 18:36
source

1 answer

-2

Returning two values in thenReturn :

Mockito.when(metodo).thenReturn(valor 1,valor 2);
    
answered by 11.01.2019 в 16:49