How can we mock the IRouteHandlerRegistry
? The error is Cannot resolve method thenReturn(IHandleRoute
public interfac
Even though TestRoute
is a subtype of RouteDefinition
, a IHandleRoute
is not a subtype of IHandleRoute
.
The when
method from Mockito returns an object of type OngoingStubbing
. This is due to the compiler inferring the type parameter TRoute
from the method
IHandleRoute getHandlerFor(TRoute route);
to be RouteDefinition
since the argument passed to getHandlerFor
is declared of type RouteDefinition
.
On the other hand, the thenReturn
method is given an argument of type IHandleRoute
whereas it expects an IHandleRoute
, that is the type argument of the OngoingStubbing
mentioned earlier. Hence the compiler error.
To solve this, the simplest way is probably to change the declaration type of route
to be TestRoute
:
TestRoute route = new TestRoute();
IRouteHandlerRegistry registry = mock(IRouteHandlerRegistry.class);
IHandleRoute handler = mock(IHandleRoute.class);
when(registry.getHandlerFor(route)).thenReturn(handler);