我正在尝试让我的一个模拟对象在调用特定方法时抛出一个已检查的Exception.我正在尝试以下方法.
@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 {
}
Run Code Online (Sandbox Code Playgroud)
但是,这会产生以下错误.
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
Run Code Online (Sandbox Code Playgroud)
看看Mockito文档,他们只使用RuntimeException,是否不可能使用Mockito从模拟对象中抛出已检查的异常?