我有这个代码,我想测试,并获得最大坑覆盖率,但我不能杀死的否定条件,如果(关闭)变种。我还使用mockito抛出一些异常。
public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException {
try {
copyBytes(in, out, buffSize);
if (close) {
out.close();
out = null;
in.close();
in = null;
}
} finally {
if (close) {
closeStream(out);
closeStream(in);
}
}
}
这是我的案例测试:
@Test
public void mockOutputCopyBytes1False(){
try{
OutputStream outputStream = Mockito.mock(OutputStream.class);
doThrow(new IOException()).when(outputStream).close();
copyBytes(createInputStream(), outputStream, 50, false);
outputStream.write(10);
}catch (Exception e){
e.printStackTrace();
Assert.assertEquals(IOException.class, e.getClass());
}
}
@Test
public void mockOutputCopyBytes1True(){
try{
OutputStream outputStream = Mockito.mock(OutputStream.class);
doThrow(new IOException()).when(outputStream).close();
copyBytes(createInputStream(), outputStream, 50, true);
outputStream.write(10);
}catch (Exception e){
e.printStackTrace();
Assert.assertEquals(IOException.class, e.getClass());
}
}
1条答案
按热度按时间at0kjp5o1#
这个
finally
无论代码是正常工作还是引发异常,都不需要为这两种情况编写代码: