@Test
public void testDownloadFile_exception() throws Exception {

    RuleDatabaseItemUpdateRunnable itemUpdateRunnable = new RuleDatabaseItemUpdateRunnable(realTask, mockContext, mock(Configuration.Item.class));
    FileOutputStream fos = mock(FileOutputStream.class);
    InputStream is = mock(InputStream.class);
    when(connection.getInputStream()).thenReturn(is);
    when(singleWriterMultipleReaderFile.startWrite()).thenReturn(fos);
    doThrow(new IOException("foobar")).when(fos, "write", any(byte[].class), anyInt(), anyInt());
    try {
        itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection);
        fail("Should have thrown exception");
    } catch (IOException e) {
        assertEquals("foobar", e.getMessage());
        assertEquals(1, failAnswer.numCalls);
        assertEquals(0, finishAnswer.numCalls);
    }
}