小编Kar*_*l K的帖子

看起来正确的doThrow中的Mockito异常

我正在尝试模拟一个方法,看看我是否正确处理异常.这是我得到的.

接口:

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)

java mockito

19
推荐指数
2
解决办法
4万
查看次数

我想模拟一个扩展 InputStream、模拟读取、验证关闭的专有类

我想使用 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)

java unit-testing inputstream mockito

1
推荐指数
1
解决办法
5158
查看次数

标签 统计

java ×2

mockito ×2

inputstream ×1

unit-testing ×1