Is there a clean method of mocking a class with generic parameters? Say I have to mock a class Foo which I need to pass into a method that expects a <
Create a test utility method. Specially useful if you need it for more than once.
@Test
public void testMyTest() {
// ...
Foo mockFooBar = mockFoo();
when(mockFooBar.getValue).thenReturn(new Bar());
Foo mockFooBaz = mockFoo();
when(mockFooBaz.getValue).thenReturn(new Baz());
Foo mockFooQux = mockFoo();
when(mockFooQux.getValue).thenReturn(new Qux());
// ...
}
@SuppressWarnings("unchecked") // still needed :( but just once :)
private Foo mockFoo() {
return mock(Foo.class);
}