How to kill negated conditional survived mutant

2.2k views Asked by At

I have this code which I want to test and get the max pit coverage but I am not able to kill the mutant for the negated condition if (close). I am also using mockito to throw some exception.

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);
            }
        }
    }

This is my case tests:

@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

There are 1 answers

1
stridecolossus On

The finally clause is executed whether the code works or throws an exception, there is no need to code for both cases:

public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException {
        try {
            copyBytes(in, out, buffSize);
            // TODO - remove the following
            //if (close) {
            //    out.close();
            //    out = null;
            //    in.close();
            //    in = null;
            //}
        } finally {
            if (close) {
                closeStream(out);
                closeStream(in);
            }
        }
    }