Search code examples
javaintegerrangemockitozero

Mockito perform anyInt() ,excluding zero


Have following expression:

when(restResponse.getStatus()).thenReturn(anyInt());

It's needed to rework this expression for anyInt() means "any Integer except 0", due to 0 is reserved for another logic.

I'm asking, because it will not graceful to specify 'magic number' within thenReturn().


Solution

  • anyInt() is not appropriate for thenReturn. anyInt is used for argument matching within when(myMock.myMethod(anyInt())). Your thenReturn should look like thenReturn(1).

    See javadocs in Matchers