【发布时间】:2011-04-15 07:00:48
【问题描述】:
我试图让我的一个模拟对象在调用特定方法时抛出一个检查异常。我正在尝试以下方法。
@Test(expectedExceptions = SomeException.class)
public void throwCheckedException() {
List<String> list = mock(List.class);
when(list.get(0)).thenThrow(new SomeException());
String test = list.get(0);
}
public class SomeException extends Exception {
}
但是,这会产生以下错误。
org.testng.TestException:
Expected exception com.testing.MockitoCheckedExceptions$SomeException but got org.mockito.exceptions.base.MockitoException:
Checked exception is invalid for this method!
Invalid: com.testing.MockitoCheckedExceptions$SomeException
看Mockito documentation,他们只使用RuntimeException,难道不能用Mockito从模拟对象抛出检查异常吗?
【问题讨论】: