Even if it TestRouteis a subtype RouteDefinition, a IHandleRoute<TestRoute>is not a subtype IHandleRoute<RouteDefinition>.
The method whenfrom Mockito returns an object of type OngoingStubbing<IHandleRoute<RouteDefinition>>. This is because the compiler infers the type parameter TRoutefrom the method
<TRoute extends RouteDefinition> IHandleRoute<TRoute> getHandlerFor(TRoute route);
will be RouteDefinitionbecause the argument passed in getHandlerForis declared type RouteDefinition.
On the other hand, the method thenReturnreceives a type argument IHandleRoute<TestRoute>, while it expects IHandleRoute<RouteDefinition>, that is, the type argument OngoingStubbingmentioned earlier. Therefore, a compiler error.
, - route TestRoute:
TestRoute route = new TestRoute();
IRouteHandlerRegistry registry = mock(IRouteHandlerRegistry.class);
IHandleRoute<TestRoute> handler = mock(IHandleRoute.class);
when(registry.getHandlerFor(route)).thenReturn(handler);