向Mockito抛出一个例外

Ahm*_*Beg 30 java mockito

我想ContentIOException从一个签名看起来像这样的方法中抛出一个.

public void putContent(InputStream is) throws ContentIOException.
Run Code Online (Sandbox Code Playgroud)

当我试图ContentIOException从Mockito那样抛出时:

when(StubbedObject.putContent(contentStream)).thenThrow(ContentIOException.class);
Run Code Online (Sandbox Code Playgroud)

我收到以下编译错误:

The method when(T) in the type Mockito is not applicable for the arguments (void).
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Mak*_*oto 45

在官方API中查看此参考.您希望反转调用方式并调整参数,因为这是一种void您希望抛出异常的方法.

doThrow(new ContentIOException()).when(StubbedObject).putContent(contentStream);
Run Code Online (Sandbox Code Playgroud)