使用doThrow()不抛出doThrow这样的异常(null,NullPointerException.class)



我需要类似doThrow(null, NullPointerException.class).when(myService).m1()的东西,以便在第一次调用void m1()时不抛出异常,在第二次调用时抛出NullPointerException

doThrow()不接受null

我该如何解决这个问题?

这样尝试:

doNothing().doThrow(NullPointerException.class).when(myService).m1();

对于非无效方法,可以采用相同的方式:

when(obj.methodName()).thenThrow(RuntimeException.class)
.thenReturn(something);

最新更新