如何使用泛型参数类型模拟方法

我有这个方法


public <T, R> R deepCopy(T source, R destination) {

   beanMapper.map(source, destination);

   return destination;

}

并想用不同的方法参数来模拟


mock.deepCopy(classA(), classB()).thenReturn(classB());

mock.deepCopy(classB(), classC()).thenReturn(classC());

但得到类转换异常。


慕妹3242003
浏览 102回答 1
1回答

30秒到达战场

这个怎么样doAnswer(invocation -> {&nbsp; &nbsp; &nbsp; &nbsp;Object arg1 = invocation.getArguments()[0];&nbsp; &nbsp; &nbsp; &nbsp;Object arg2 = invocation.getArguments()[1];&nbsp; &nbsp; &nbsp; &nbsp;if(arg1 instanceof Integer && arg2 instanceof String)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return "something";&nbsp; &nbsp; &nbsp; &nbsp; if(arg1 instanceof String && arg2 instanceof Boolean)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; return false;}).when(yourmock).deepCopy(any(), any());现在,如果您使用参数 (1, "abcd") 传递 call 方法,则模拟将返回“某物”。如果你通过 ("abcd", true) 那么它返回 false
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java