为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕妹3242003 2021-06-22 17:05:26
我有这个方法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());但得到类转换异常。
查看完整描述

1 回答

?
30秒到达战场

TA贡献1828条经验 获得超6个赞

这个怎么样


doAnswer(invocation -> {

       Object arg1 = invocation.getArguments()[0];

       Object arg2 = invocation.getArguments()[1];


       if(arg1 instanceof Integer && arg2 instanceof String)

           return "something";

        if(arg1 instanceof String && arg2 instanceof Boolean)

            return false;


        return false;

}).when(yourmock).deepCopy(any(), any());

现在,如果您使用参数 (1, "abcd") 传递 call 方法,则模拟将返回“某物”。如果你通过 ("abcd", true) 那么它返回 false


查看完整回答
反对 回复 2021-06-30
  • 1 回答
  • 0 关注
  • 101 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信