@Test public void testWriterFailure() throws Exception { // create a writer that will fail immediately AbstractCsvWriter csvWriter = new MyCsvWriter(failingList); Thread thread = new Thread(csvWriter); thread.setDaemon(true); // make sure thread dies when our test is done thread.start(); // read from it long start = System.currentTimeMillis(); byte[] bytes = StreamUtil.readAsBytes(csvWriter.getReadStream()); Assert.assertEquals(22, bytes.length); Assert.assertNotNull(csvWriter.getError()); Assert.assertEquals(FAIL_MESSAGE, csvWriter.getError().getMessage()); // make sure it fails within a couple seconds Assert.assertTrue(System.currentTimeMillis() - start < 2000); }