### Question:
AutomaticExporter { public boolean tryExport() throws AutomaticExportException { if (businessPreferenceGetter.getAutomaticExport()) { exportTreeManager.prepareTree(); if (exportTreeManager.isExportNeeded()) { FileWriter nextExportFile; try { nextExportFile = exportTreeManager.getNextExportFile(); } catch (IOException e) { throw new AutomaticExportException(e, R.string.automatic_export_cant_get_next_export); } try { csvExporterFactory.makeCsvExporter(nextExportFile).export(); exportTreeManager.clean(); } catch (IOException e) { throw new AutomaticExportException(e, R.string.automatic_export_cant_export); } return true; } } return false; } AutomaticExporter(Application application, ExporterFactory exporterFactory, String subDir);   AutomaticExporter(ExportTreeManager exportTreeManager, BusinessPreferenceGetter businessPreferenceGetter, ExporterFactory exporterFactory); boolean tryExport();  }### Answer:
@Test public void tryExportNotEnabled() throws AutomaticExportException { doReturn(false).when(businessPreferenceGetter).getAutomaticExport(); assertFalse(new AutomaticExporter(exportTreeManager, businessPreferenceGetter, csvExporterFactory).tryExport()); verify(exportTreeManager, never()).prepareTree(); }
@Test public void tryExportNotNeeded() throws IOException, AutomaticExportException { doReturn(true).when(businessPreferenceGetter).getAutomaticExport(); doReturn(false).when(exportTreeManager).isExportNeeded(); assertFalse(new AutomaticExporter(exportTreeManager, businessPreferenceGetter, csvExporterFactory).tryExport()); verify(exportTreeManager).prepareTree(); verify(exportTreeManager, never()).getNextExportFile(); }
@Test public void tryExportIOWhenGetNextExportFile() throws IOException, AutomaticExportException { expectedException.expect(AutomaticExportException.class); expectedException.expect(hasProperty("stringCode", is(R.string.automatic_export_cant_get_next_export))); doReturn(true).when(businessPreferenceGetter).getAutomaticExport(); doReturn(true).when(exportTreeManager).isExportNeeded(); doThrow(IOException.class).when(exportTreeManager).getNextExportFile(); new AutomaticExporter(exportTreeManager, businessPreferenceGetter, csvExporterFactory).tryExport(); }
@Test public void tryExportIOWhenExport() throws IOException, AutomaticExportException { expectedException.expect(AutomaticExportException.class); expectedException.expect(hasProperty("stringCode", is(R.string.automatic_export_cant_export))); doReturn(true).when(businessPreferenceGetter).getAutomaticExport(); doReturn(true).when(exportTreeManager).isExportNeeded(); FileWriter fileWriter = mock(FileWriter.class); doReturn(fileWriter).when(exportTreeManager).getNextExportFile(); CsvExporter csvExporter = mock(CsvExporter.class); doReturn(csvExporter).when(csvExporterFactory).makeCsvExporter(fileWriter); doThrow(IOException.class).when(csvExporter).export(); assertTrue(new AutomaticExporter(exportTreeManager, businessPreferenceGetter, csvExporterFactory).tryExport()); verify(exportTreeManager, never()).clean(); }
@Test public void tryExport() throws IOException, AutomaticExportException { doReturn(true).when(businessPreferenceGetter).getAutomaticExport(); doReturn(true).when(exportTreeManager).isExportNeeded(); FileWriter fileWriter = mock(FileWriter.class); doReturn(fileWriter).when(exportTreeManager).getNextExportFile(); CsvExporter csvExporter = mock(CsvExporter.class); doReturn(csvExporter).when(csvExporterFactory).makeCsvExporter(fileWriter); assertTrue(new AutomaticExporter(exportTreeManager, businessPreferenceGetter, csvExporterFactory).tryExport()); verify(exportTreeManager).prepareTree(); verify(csvExporter).export(); verify(exportTreeManager).clean(); }