Accéder au contenu principal

Articles

Affichage des articles du mai, 2022

Create and use a result captor in Mockito

Today I will show you how to create a Mockito captor In Mockito it exists the possibilty to use ArgumentCaptor to allow developers to verify the arguments used during the call of mocked method, but not the result itself. Indeed, in the current release of Mockito it's not possible to capture it and my solution to do that is to build a ResultCaptor class which implements the Answer interface and generify it for more conveniance. Let's take a look. Create the ResultCaptor class public static class ResultCaptor < T > implements Answer < T > { private T result = null ; public T getResult () { return result ; } @Override public T answer ( InvocationOnMock invocationOnMock) throws Throwable { //noinspection unchecked result = ( T ) invocationOnMock.callRealMethod(); return result ; } } In this quite simple implementation we see that the ResultCaptor class implements the Answer interface which force to override the