【发布时间】:2012-06-08 16:26:09
【问题描述】:
我正在使用 Mockito 编写一个使用 java.beans.PropertyDescriptor 的测试用例,并且我想模拟 getPropertyType() 的行为以返回任意 Class<?> 对象(在我的情况下为 String.class)。通常,我会通过调用来做到这一点:
// we already did an "import static org.mockito.Mockito.*"
when(mockDescriptor.getPropertyType()).thenReturn(String.class);
然而,奇怪的是,这并不能编译:
cannot find symbol method thenReturn(java.lang.Class<java.lang.String>)
但是当我指定类型参数而不是依赖推断时:
Mockito.<Class<?>>when(mockDescriptor.getPropertyType()).thenReturn(String.class);
一切都是笨拙的多莉。在这种情况下,为什么编译器不能正确推断 when() 的返回类型?我以前从来没有像那样指定参数。
【问题讨论】: