How to capture an argument with Mockito and execute real method?

I have some integration tests that use Mockito, and they are working fine.

However, I want to verify that the input of a method that is called some time during the process I’m testing.
The thing is: I don’t want to mock the method behavior, only to inspect its argument and execute the real method.
If I do something like this:

@MockBean
private ClassThatIsCalled classThatIsCalled;
@Captor
private ArgumentCaptor<TypeOfMyParameter> typeOfMyParameter;

I get an error because a dependency of ClassThatIsCalled was not found. Even If I do given(classThatIsCalled.callMethod(any())).willCallRealMethod();
I’m still mocking ClassThatIsCalled which is not what I want to do.

Is there a way to call the productive code of ClassThatIsCalled (without mocking it) and simply inspect the passed argument?

TL;DR: I want to use @Spy/@Captor/similar to inspect an argument passed to a class, but without mocking the class, but calling its real method (using .willCallRealMethod() is not enough, since a mocked class has no dependencies injected).

Leave a Comment