我正在尝试模拟一个方法,看看我是否正确处理异常.这是我得到的.
接口:
interface SampleManager {
void deleteVariome(String specimenId, String analysisId) throws Exception;
// ...
}
Run Code Online (Sandbox Code Playgroud)
单元测试:
// ...
SampleManger sampleManager = mock(SampleManager.class);
// below is line 753
doThrow(Exception.class).when(sampleManager).deleteVariome(sample1.getId(), analysisId);
Run Code Online (Sandbox Code Playgroud)
结果:
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.
Run Code Online (Sandbox Code Playgroud)
Gij*_*ijs 20
从我遇到的一个相同的问题,我怀疑这sample是一个模拟,你sample.getId()在其他地方存根?无论如何,这在我的情况下造成了这个问题.
出于某种原因,如果你传递给以doThrow这种方式使用的存根的一个参数是你也嘲笑的方法的结果,Mockito会感到沮丧.我不知道,这可能是为了避免无限循环而进行的重新检查.
无论如何,尝试用sample.getId()恒定值替换,这应该解决问题.你可以考虑在你的测试中使用一个常量来模拟它和它的任何进一步用途.然后,sample.getId()您还可以通过添加另一个调用来检查您正在测试的方法使用的verify.
您必须提供Exception.class 的实例,而不是 Exception 类本身。
doThrow(new Exception()).when(sampleManager).deleteVariome(sample1.getId(), analysisId);
Run Code Online (Sandbox Code Playgroud)
编辑 @DavidWallace 纠正了我,所以请注意(或者更确切地说是开明),从 1.9 开始,您只需提供要抛出的异常类,它就会为您构造一个异常类。
| 归档时间: |
|
| 查看次数: |
42989 次 |
| 最近记录: |