### Question:
ExternalProcessRunner { public ProcessResult executeAndThrowOnFailure(String command, String... args) throws ProcessNotExecutableException, ProcessFailedException, InterruptedException { ProcessResult processResult = execute(command, args); if (processResult.getExitCode() != 0) { String processOutputString = processOutputToString(processResult.processOutput); String errorMsg = String.format("Failed to run command '%s' with output: %n %s", processResult.commandWithArgs, processOutputString); log.error(errorMsg); throw new ProcessFailedException(errorMsg, processResult.processOutput); } return processResult; }  ProcessResult execute(String command, String... args); ProcessResult executeAndThrowOnFailure(String command, String... args); static String processOutputToString(List<String> processOutput);  }### Answer:
@Test public void executeAndThrowOnFailureSuccess() { try { ExternalProcessRunner.ProcessResult processResult = externalProcessRunner.executeAndThrowOnFailure(MOCK_SUCCESS_SCRIPT, SCRIPT_ARGS); assertEquals(MOCK_SUCCESS_SCRIPT + " " + SCRIPT_ARGS, processResult.getCommandWithArgs()); assertEquals(0, processResult.getExitCode()); assertEquals("SUCCESS", processResult.getProcessOutput().get(0)); } catch (Exception e) { fail("should not throw exceptions"); } }
@Test public void executeAndThrowOnFailureScriptFail() throws Exception { try { externalProcessRunner.executeAndThrowOnFailure(MOCK_FAIL_SCRIPT, SCRIPT_ARGS); } catch (ProcessFailedException e) { assertEquals(PROCESS_FAILED, e.getErrorDeviation().getCode()); } }
@Test public void executeAndThrowOnFailureError() throws Exception { try { externalProcessRunner.executeAndThrowOnFailure(NON_EXISTING_SCRIPT, SCRIPT_ARGS); } catch (ProcessNotExecutableException e) { assertTrue(e.getCause() instanceof IOException); } }