### Question:
ReplayCommand implements Command { @Override public void run() { Optional<Process> virtualScreenProcessOptional = Optional.empty(); try { if (screenNumber != 0) { virtualScreenProcessOptional = Optional.of(virtualScreenProcessCreator.createXvfbProcess(screenNumber)); } TestScenarioSteps testCaseSteps = stepsProvider.get(); ReplayBrowser replayBrowser = replayBrowserProvider.get(); ExecutionReport executionReport = replayBrowser.replay(testCaseSteps); executionReport.getLoadingTimes().dump(measurementsFile); executionReport.getHar().writeTo(harFile); logger.info("Test run result {}", executionReport.getAutomationResult().toString()); } catch (IOException | URISyntaxException e) { logger.error(e.getMessage(), e); } finally { virtualScreenProcessOptional.ifPresent(Process::destroy); } } @Inject  ReplayCommand(IOURIProvider<ReplayBrowser> replayBrowserProvider,
                         IOProvider<TestScenarioSteps> stepsProvider,
                         VirtualScreenProcessCreator virtualScreenProcessCreator,
                         @Named(SCREEN_NUMBER) Integer screenNumber,
                         @Named(MEASUREMENTS_FILE) File measurementsFile,
                         @Named(HAR_FILE) File harFile); @Override void run();  }### Answer:
@Test public void cleansUpEvenIfExceptionIsThrown() throws IOException, URISyntaxException { String exceptionMessage = "Browser could not be created"; when(replayBrowserProvider.get()).thenThrow(new IOException(exceptionMessage)); TestScenarioSteps steps = new TestScenarioSteps(); when(stepsProvider.get()).thenReturn(steps); when(executionReport.getAutomationResult()).thenReturn(AutomationResult.COULD_NOT_CREATE_DRIVER); VirtualScreenProcessCreator virtualScreenProcessCreator = mock(VirtualScreenProcessCreator.class); Integer screenNumber = 1; Process process = mock(Process.class); when(virtualScreenProcessCreator.createXvfbProcess(screenNumber)).thenReturn(process); ReplayCommand replayCommand = new ReplayCommand(replayBrowserProvider, stepsProvider, virtualScreenProcessCreator, screenNumber, measurementsFile, harFile); replayCommand.run(); }
@Test public void executesStepsWithTheSuppliedBrowser() throws IOException, URISyntaxException { when(replayBrowserProvider.get()).thenReturn(replayBrowser); TestScenarioSteps steps = new TestScenarioSteps(); when(stepsProvider.get()).thenReturn(steps); when(executionReport.getAutomationResult()).thenReturn(AutomationResult.SUCCESS); Process process = mock(Process.class); VirtualScreenProcessCreator virtualScreenProcessCreator = mock(VirtualScreenProcessCreator.class); Integer screenNumber = 1; when(virtualScreenProcessCreator.createXvfbProcess(screenNumber)).thenReturn(process); when(replayBrowser.replay(steps)).thenReturn(executionReport); ReplayCommand replayCommand = new ReplayCommand(replayBrowserProvider, stepsProvider, virtualScreenProcessCreator, screenNumber, measurementsFile, harFile); replayCommand.run(); verify(replayBrowser).replay(steps); }
@Test public void executesOnDefaultScreenIfSetToZero() throws IOException, URISyntaxException { when(replayBrowserProvider.get()).thenReturn(replayBrowser); TestScenarioSteps steps = new TestScenarioSteps(); when(stepsProvider.get()).thenReturn(steps); when(executionReport.getAutomationResult()).thenReturn(AutomationResult.SUCCESS); VirtualScreenProcessCreator virtualScreenProcessCreator = mock(VirtualScreenProcessCreator.class); Integer screenNumber = 0; when(replayBrowser.replay(steps)).thenReturn(executionReport); ReplayCommand replayCommand = new ReplayCommand(replayBrowserProvider, stepsProvider, virtualScreenProcessCreator, screenNumber, measurementsFile, harFile); replayCommand.run(); verify(replayBrowser).replay(steps); }