如何将一个以Generic类作为参数的方法存根?

时间:2014-08-08 17:12:26

标签: java junit mocking mockito

我有一个正在测试的类需要在依赖类上存根调用。需要存根的代码如下所示:

public class A {
 ...
 public void methodThatNeedsToBeStubbed(GenericClass genericClass){
    ...
 }
}

这不编译:

when(mockA.methodThatNeedsToBeStubbed(any(GenericClass<SomeDifferentClass>))

我不确定如何使用泛型类作为参数?

1 个答案:

答案 0 :(得分:2)

尝试将ArgumentCaptor@Captor一起使用。

 @Captor
 ArgumentCaptor<GenericClass<SomeDifferentClass>> captor;

 @Before
 public void setup(){ MockitoAnnotations.initMocks(this));}

 @Test
 public void test(){
   when(mockA.methodThatNeedsToBeStubbed(captor.capture()))...
 }
相关问题