WakeOnLanInstallation implements InitializingBean { protected void installWakeOnLan() { logger.info("Checking if wakeonlan is installed."); File file = new File(wakeOnLanCommand); boolean isWakeOnLanInstalled = file.exists(); if (isWakeOnLanInstalled) { logger.info("Wakeonlan is already installed."); return; } logger.info("Wakeonlan is not installed."); logger.info("Installing wakeonlan."); String logInstallation = shellCommandUtils.executeCommand(String.format("%s", aptitudeInstallWakeOnLan)); logger.info(logInstallation); logger.info("Installation finished."); }  @Override void afterPropertiesSet();  }
@Test @PrepareForTest(WakeOnLanInstallation.class) public void installWakeOnLanTestIfNotInstalled() throws Exception { prepareMockedFileThatRepresentsTheWakeOnLanCommand(false); String command = "aptitude -y install wakeonlan"; final String returnOfInstallationCommand = "return of command"; Mockito.when(wakeOnLanInstallation.shellCommandUtils.executeCommand(command)).thenReturn(returnOfInstallationCommand); wakeOnLanInstallation.installWakeOnLan(); InOrder inOrder = Mockito.inOrder(wakeOnLanInstallation.shellCommandUtils, wakeOnLanInstallation.logger); inOrder.verify(wakeOnLanInstallation.logger).info("Checking if wakeonlan is installed."); inOrder.verify(wakeOnLanInstallation.logger).info("Wakeonlan is not installed."); inOrder.verify(wakeOnLanInstallation.logger).info("Installing wakeonlan."); inOrder.verify(wakeOnLanInstallation.shellCommandUtils).executeCommand(command); inOrder.verify(wakeOnLanInstallation.logger).info(returnOfInstallationCommand); inOrder.verify(wakeOnLanInstallation.logger).info("Installation finished."); Mockito.verify(wakeOnLanInstallation.logger, Mockito.times(0)).info("Wakeonlan is already installed."); }
@Test @PrepareForTest(WakeOnLanInstallation.class) public void installWakeOnLanTestWakeOnLanAlreadyInstalled() throws Exception { prepareMockedFileThatRepresentsTheWakeOnLanCommand(true); wakeOnLanInstallation.installWakeOnLan(); Mockito.verify(wakeOnLanInstallation.logger).info("Checking if wakeonlan is installed."); Mockito.verify(wakeOnLanInstallation.logger).info("Wakeonlan is already installed."); Mockito.verify(wakeOnLanInstallation.shellCommandUtils, Mockito.times(0)).executeCommand(Mockito.anyString()); }