### Question:
PmdSensor implements Sensor { @Override public void analyse(Project project, SensorContext context) { try { Report report = executor.execute(); for (RuleViolation violation : report) { pmdViolationRecorder.saveViolation(violation); } } catch (Exception e) { throw new XmlParserException(e); } } PmdSensor(RulesProfile profile, PmdExecutor executor, PmdViolationRecorder pmdViolationRecorder, FileSystem fs); @Override boolean shouldExecuteOnProject(Project project); @Override void analyse(Project project, SensorContext context); @Override String toString();  }### Answer:
@Test public void should_report_violations() { RuleViolation pmdViolation = violation(); Report report = report(pmdViolation); when(executor.execute()).thenReturn(report); pmdSensor.analyse(project, sensorContext); verify(pmdViolationRecorder).saveViolation(pmdViolation); }
@Test public void shouldnt_report_zero_violation() { Report report = report(); when(executor.execute()).thenReturn(report); pmdSensor.analyse(project, sensorContext); verifyZeroInteractions(sensorContext); }
@Test public void shouldnt_report_invalid_violation() { RuleViolation pmdViolation = violation(); Report report = report(pmdViolation); when(executor.execute()).thenReturn(report); when(report.iterator()).thenReturn(Iterators.forArray(pmdViolation)); pmdSensor.analyse(project, sensorContext); verifyZeroInteractions(sensorContext); }
@Test public void should_report_analyse_failure() { when(executor.execute()).thenThrow(new RuntimeException()); exception.expect(XmlParserException.class); pmdSensor.analyse(project, sensorContext); }