### Question:
RootKiller implements ProcessKiller { public boolean kill(Process process) { if (RXSDebug.isDebug()) Timber.tag(TAG).d("kill(%s)", process); if (!ProcessHelper.isAlive(process)) { if (RXSDebug.isDebug()) Timber.tag(TAG).d("Process is no longer alive, skipping kill."); return true; } Matcher matcher = PID_PATTERN.matcher(process.toString()); if (!matcher.matches()) { if (RXSDebug.isDebug()) Timber.tag(TAG).e("Can't find PID for %s", process); return false; } int pid = Integer.parseInt(matcher.group(1)); List<Integer> allRelatedPids = getAllPids(pid); if (RXSDebug.isDebug()) Timber.tag(TAG).d("Related pids: %s", allRelatedPids); if (allRelatedPids != null && destroyPids(allRelatedPids)) { return true; } else { if (RXSDebug.isDebug()) Timber.tag(TAG).w("Couldn't destroy process via root shell, trying Process.destroy()"); process.destroy(); return false; } } RootKiller(ProcessFactory processFactory); boolean kill(Process process);  }### Answer:
@Test public void testKill() { ApiWrap.setSDKInt(26); when(processtoKill.isAlive()).thenReturn(true); when(processtoKill.toString()).thenReturn("Process[pid=1234, hasExited=false]"); ProcessKiller killer = new RootKiller(processFactory); psProcess.addCmdListener(line -> { if (line.startsWith("ps")) { psProcess.printData(" PID PPID PGID WINPID TTY UID STIME COMMAND"); psProcess.printData(" 5678 1234 6316 7860 pty1 1001 Nov 4 /usr/bin/ssh"); } return true; }); assertThat(ProcessHelper.isAlive(processtoKill), is(true)); assertThat(killer.kill(processtoKill), is(true)); assertThat(killProcess.getLastCommandRaw(), is("kill 1234" + LineReader.getLineSeparator())); }
@Test public void testKill_dontKillTheDead() throws IOException { ApiWrap.setSDKInt(26); when(processtoKill.isAlive()).thenReturn(false); ProcessKiller killer = new RootKiller(processFactory); assertThat(killer.kill(processtoKill), is(true)); verify(processFactory, never()).start(any()); }
@Test public void testKill_cantFindProcessPid() throws IOException { ApiWrap.setSDKInt(26); when(processtoKill.isAlive()).thenReturn(true); ProcessKiller killer = new RootKiller(processFactory); when(processtoKill.toString()).thenReturn(""); assertThat(psProcess.getLastCommandRaw(), is(nullValue())); assertThat(killer.kill(processtoKill), is(false)); }