Using Mockito to mock classes with generic parameters

I think you do need to cast it, but it shouldn’t be too bad:

Foo<Bar> mockFoo = (Foo<Bar>) mock(Foo.class);
when(mockFoo.getValue()).thenReturn(new Bar());

Leave a Comment