### Question:
PmdConfiguration { Path dumpXmlReport(Report report) { if (!settings.getBoolean(PROPERTY_GENERATE_XML).orElse(false)) { return null; } try { final String reportAsString = reportToString(report); final Path reportFile = writeToWorkingDirectory(reportAsString, PMD_RESULT_XML); LOG.info("PMD output report: " + reportFile.toString()); return reportFile; } catch (IOException e) { throw new IllegalStateException("Fail to save the PMD report", e); } } PmdConfiguration(FileSystem fileSystem, Configuration settings);   }### Answer:
@Test void should_dump_xml_report() throws IOException { when(fs.workDir()).thenReturn(WORK_DIR); settings.setProperty(PmdConfiguration.PROPERTY_GENERATE_XML, true); Path reportFile = configuration.dumpXmlReport(new Report()); assertThat(reportFile.toFile()).isEqualTo(new File(WORK_DIR, "pmd-result.xml")); List<String> writtenLines = Files.readAllLines(reportFile, StandardCharsets.UTF_8); assertThat(writtenLines).hasSize(6); assertThat(writtenLines.get(1)).contains("<pmd"); }
@Test void should_fail_to_dump_xml_report() { when(fs.workDir()).thenReturn(new File("xxx")); settings.setProperty(PmdConfiguration.PROPERTY_GENERATE_XML, true); final Throwable thrown = catchThrowable(() -> configuration.dumpXmlReport(new Report())); assertThat(thrown) .isInstanceOf(IllegalStateException.class) .hasMessage("Fail to save the PMD report"); }
@Test void should_ignore_xml_report_when_property_is_not_set() { Path reportFile = configuration.dumpXmlReport(new Report()); assertThat(reportFile).isNull(); verifyNoMoreInteractions(fs); }
@Test void should_dump_xml_report() throws IOException { when(fs.workDir()).thenReturn(WORK_DIR); settings.setProperty(PmdConfiguration.PROPERTY_GENERATE_XML, true); Path reportFile = configuration.dumpXmlReport(new Report()); assertThat(reportFile.toFile()).isEqualTo(new File(WORK_DIR, "pmd-result.xml")); List<String> writtenLines = Files.readAllLines(reportFile, StandardCharsets.UTF_8); assertThat(writtenLines).hasSize(3); assertThat(writtenLines.get(1)).contains("<pmd"); }
@Test void should_ignore_xml_report_when_property_is_not_set() { Path reportFile = configuration.dumpXmlReport(new Report()); assertThat(reportFile).isNull(); verifyZeroInteractions(fs); }