java 泛型 边界,Mockito.when()和泛型类型推断的一个奇怪的泛型边界案例

在使用Mockito测试java.beans.PropertyDescriptor时遇到一个问题,调用when(mockDescriptor.getPropertyType()).thenReturn(String.class)无法编译。原因是getPropertyType()返回的是Class,而thenReturn需要Class。解决办法是显式指定类型参数,如Mockito.>when(mockDescriptor.getPropertyType()).thenReturn(String.class),这样编译就能通过。
摘要由CSDN通过智能技术生成

I'm writing a test case that uses a java.beans.PropertyDescriptor using Mockito, and I want to mock the behavior of getPropertyType() to return an arbitrary Class> object (in my case, String.class). Normally, I would do that by just invoking:

// we already did an "import static org.mockito.Mockito.*"

when(mockDescriptor.getPropertyType()).thenReturn(String.class);

However, oddly, this does not compile:

cannot find symbol method thenReturn(java.lang.Class)

But when I specify the type parameter instead of depending on inference:

Mockito.>when(mockDescriptor.getPropertyType()).thenReturn(String.class);

everything is hunky dory. Why can't the compiler correctly infer the return type of when() in this case? I have never had to specify the parameter before like that.

解决方案

PropertyDescriptor#getPropertyType() re

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值