SshUtils implements InitializingBean { public void executeCommandOnHostViaSsh(String hostIp, String command) { Connection sshConnectionWithHost = getSshConnectionWithHost(hostIp); try { Session sshSession = authenticateSshSessionWithPublicKey(sshConnectionWithHost); sshSession.execCommand(command); waitUntilCommandFinishes(hostIp, command, sshSession); sshSession.close(); } catch (IOException e) { logger.info("Unable to execute command: " + command, e); } sshConnectionWithHost.close(); }  void executeCommandOnHostViaSsh(String hostIp, String command); void sendFileToHost(File localfilePath, String remoteFileName, String remotePath, String address); @Override void afterPropertiesSet();  }
@Test public void executeCommandOnHostViaSshTest() throws Exception { configureExecuteCommandOnHostViaSshTest(sshConnectionWithHost, sshSession); spy.executeCommandOnHostViaSsh("hostIp", "command"); verifyExecuteCommandOnHostViaSshTestCalledMethods(sshConnectionWithHost, sshSession, 1); }
@Test public void executeCommandOnHostViaSshTestCatchIOException() throws Exception { configureExecuteCommandOnHostViaSshTest(sshConnectionWithHost, sshSession); Mockito.doThrow(IOException.class).when(sshSession).execCommand(Mockito.eq("command")); spy.executeCommandOnHostViaSsh("hostIp", "command"); verifyExecuteCommandOnHostViaSshTestCalledMethods(sshConnectionWithHost, sshSession, 0); }