Using Mockito to mock classes with generic parameters

后端 未结 7 1890
不知归路
不知归路 2020-11-30 19:37

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 <

7条回答
  •  眼角桃花
    2020-11-30 19:42

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

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

提交回复
热议问题