我正在尝试模拟一个方法,看看我是否正确处理异常.这是我得到的.
接口:
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 …Run Code Online (Sandbox Code Playgroud) 我想使用 Mockito 来模拟 AmazonS3 并测试从中打开一个流,然后在我的代码读取它后验证该流是否已关闭。我还想从流中读取字节。像这样的东西:
AmazonS3 client = mock(AmazonS3.class);
when(tm.getAmazonS3Client()).thenReturn(client);
S3Object response = mock(S3Object.class);
when(client.getObject(any(GetObjectRequest.class))).thenReturn(response);
S3ObjectInputStream stream = mock(S3ObjectInputStream.class);
when(response.getObjectContent()).thenReturn(stream);
somehow mock the read method
MyObject me = new MyObject(client);
byte[] bra me.getBytes(File f, offset, length);
assertEquals(length, bra.length);
verify(stream).close();
Run Code Online (Sandbox Code Playgroud)