要进行单元测试的方法的一部分如下
SomeTypeCollection<SomeType> someTypeCollection = ...
T currentObject = null;
while( ( currentObject = (T) someTypeCollection.next() ) != null ) {...}
单元测试的相关部分将是
@Mock
SomeTypeCollection<SomeType> someTypeCollectionMock;
@Mock
SomeType someTypeMock;
when(someTypeCollectionMock.next()).thenReturn(someTypeMock);
然而,虽然模拟的 next() 调用似乎成功返回了模拟的对象,但转换失败并出现以下错误
> SomeType$$EnhancerByMockitoWithCGILIB$$98474372 无法转换为 (ActualTypeOfCurrentObject)