FileReporter { public void report(String content) throws ReportException { try (Writer writer = new BufferedWriter(new OutputStreamWriter(getOutputStream(), encoding))) { writer.write(content); } catch (IOException ioe) { throw new ReportException(ioe); } } FileReporter(String encoding);  FileReporter(File reportFile);  FileReporter(File reportFile, String encoding); void report(String content);  }
@Test public void testEmptyReport() throws ReportException { File reportFile = new File("report.tmp"); FileReporter fileReporter = new FileReporter(reportFile); fileReporter.report(""); assertTrue(reportFile.exists()); assertEquals(0L, reportFile.length()); assertTrue(reportFile.delete()); }
@Test public void testReport() throws ReportException, IOException { String testString = "first line\nsecond line"; File reportFile = new File("report.tmp"); FileReporter fileReporter = new FileReporter(reportFile); fileReporter.report(testString); assertEquals(testString, readFile(reportFile)); assertTrue(reportFile.delete()); }
@Test(expected = ReportException.class) public void testInvalidFile() throws ReportException { File reportFile = new File("/invalid_folder/report.tmp"); FileReporter fileReporter = new FileReporter(reportFile); fileReporter.report(""); }