我要测试的方法有一个 for 循环,其中包含 bList 中每个元素的逻辑:
class A {
void someMethod(){
for(B b: bList){
//some logic for b
}
}
}
执行以下测试时出现异常:
@RunWith(MockitoJUnitRunner.class)
class ATest {
@Mock
private B b;
@Mock
private Map<Int, List<B>> bMap;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private List<B> bList;
@Spy
@InjectMocks
private C c;
....
@Test
public void test(){
//this line executes fine
when(bList.size()).thenReturn(1);
//strangely this works fine
when(bMap.get(any())).thenReturn(bList);
//ClassCastException
when(bList.get(0)).thenReturn(b); // or when(bList.get(anyInt())).thenReturn(b);
c.methodIWantToTest();
}
}
我得到的例外是:
java.lang.ClassCastException:
org.mockito.internal.creation.jmock.ClassImposterizer$ClassWithSuperclassToWorkAroundCglibBug$$EnhancerByMockitoWithCGLIB$$ cannot be cast to xyz.B
有没有人遇到过这种情况并提出解决方法?
我已经搜索了一个解决方案并遇到了一些链接: http ://code.google.com/p/mockito/issues/detail?id=251 和 http://code.google.com/p/mockito/issues /detail?id=107