SingleWriterMultipleReaderFile { public FileOutputStream startWrite() throws IOException { if (workFile.exists() && !workFile.delete()) throw new IOException("Cannot delete working file"); return new FileOutputStream(workFile); } SingleWriterMultipleReaderFile(File file); InputStream openRead(); FileOutputStream startWrite(); void finishWrite(FileOutputStream stream); void failWrite(FileOutputStream stream);  }
@Test @PrepareForTest({FileOutputStream.class, SingleWriterMultipleReaderFile.class}) public void testStartWrite_success() throws Exception { when(reader.startWrite()).thenCallRealMethod(); when(workFile.exists()).thenReturn(false); when(workFile.getPath()).thenReturn("/nonexisting/path/for/dns66"); whenNew(FileOutputStream.class).withAnyArguments().thenReturn(fos); assertSame(fos, reader.startWrite()); }
@Test public void testStartWrite_failDelete() throws Exception { when(reader.startWrite()).thenCallRealMethod(); when(workFile.exists()).thenReturn(true); when(workFile.delete()).thenReturn(false); try { reader.startWrite(); fail("Failure to delete work file not detected"); } catch (IOException e) { } }