我想模拟一个方法,看看我是否正确处理异常。这是我得到的。在doThrow Mockito异常看起来是正确的
接口:
interface SampleManager {
void deleteVariome(String specimenId, String analysisId) throws Exception;
// ...
}
单元测试:
// ...
SampleManger sampleManager = mock(SampleManager.class);
// below is line 753
doThrow(Exception.class).when(sampleManager).deleteVariome(sample1.getId(), analysisId);
结果:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at ...server.ArchiveManagerImplUTest.deleteVariomeFails(ArchiveManagerImplUTest.java:753)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod(); <-- this looks a log like what I did!
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer! <-- I have a lot of other mocks of this interface in this test that work.
不,从1.9.0的Mockito起,您可以提供异常类,这将作为预期。但是,即使OP使用旧版本的Mockito,他们也不会在这里描述他们描述的症状。我相信这个问题是不同的。 –
谢谢大卫。不知道。我会在这里留下我的回答,以便其他人可以通过您的评论意识到。 –