@Test
public void testDownloadFile() throws Exception {

    RuleDatabaseItemUpdateRunnable itemUpdateRunnable = new RuleDatabaseItemUpdateRunnable(realTask, mockContext, mock(Configuration.Item.class));
    byte[] bar = new byte[]{'h', 'a', 'l', 'l', 'o'};
    final ByteArrayInputStream bis = new ByteArrayInputStream(bar);
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    FileOutputStream fos = mock(FileOutputStream.class);
    when(fos, "write", any(byte[].class), anyInt(), anyInt()).then(new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            byte[] buffer = invocation.getArgumentAt(0, byte[].class);
            int off = invocation.getArgumentAt(1, Integer.class);
            int len = invocation.getArgumentAt(2, Integer.class);
            bos.write(buffer, off, len);
            return null;
        }
    });
    when(connection.getInputStream()).thenReturn(bis);
    when(singleWriterMultipleReaderFile.startWrite()).thenReturn(fos);
    itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection);
    assertArrayEquals(bar, bos.toByteArray());
    assertEquals(0, failAnswer.numCalls);
    assertEquals(1, finishAnswer.numCalls);
    bis.reset();
    bos.reset();
}