Source:
<code>public void shutdownAppCommandUsesBestEffortApproach() throws Exception { ApexCli cliUnderTest = new ApexCli(); StramAgent stramAgent = mock(StramAgent.class); YarnClient yarnClient = mock(YarnClient.class); Whitebox.setInternalState(cliUnderTest, "stramAgent", stramAgent); Whitebox.setInternalState(cliUnderTest, "yarnClient", yarnClient); Whitebox.setInternalState(cliUnderTest, "consolePresent", true); suppress(constructor(WebServicesClient.class, new Class[0])); List<ApplicationReport> runningApplications = new ArrayList<>(); ApplicationReport app1 = mockRunningApplicationReport("application-id-1", "app1"); ApplicationReport app2 = mockRunningApplicationReport("application-id-2", "app2"); runningApplications.add(app1); runningApplications.add(app2); when(yarnClient.getApplications(Mockito.any(Set.class))).thenReturn(runningApplications); when(stramAgent.issueStramWebRequest( Mockito.any(WebServicesClient.class), Mockito.anyString(), Mockito.any(StramAgent.StramUriSpec.class), Mockito.any(WebServicesClient.WebServicesHandler.class))) .thenReturn(new JSONObject()); final ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); final ByteArrayOutputStream stdErr = new ByteArrayOutputStream(); PrintStream beforeOut = System.out; PrintStream beforeErr = System.err; System.setOut(new PrintStream(stdOut)); System.setErr(new PrintStream(stdErr)); String shutdownAppsCommand = "shutdown-app app1 notExisting app2"; cliUnderTest.processLine(shutdownAppsCommand, new ConsoleReader(), true); <START> <END> Assert.assertEquals( "Shutdown of app application-id-1 requested: {}\nShutdown of app application-id-2 requested: {}\n", stdOut.toString() ); Assert.assertEquals( "Failed to request shutdown for app notExisting: Application with id or name notExisting not found\n", stdErr.toString() ); System.setOut(beforeOut); System.setErr(beforeErr); }</code><technical_language>a good idea flush current System.out System.err (or close them) ensure get complete/consistent output in stdOut stdErr</technical_language>

Target:
public void shutdownAppCommandUsesBestEffortApproach() throws Exception { ApexCli cliUnderTest = new ApexCli(); StramAgent stramAgent = mock(StramAgent.class); YarnClient yarnClient = mock(YarnClient.class); Whitebox.setInternalState(cliUnderTest, "stramAgent", stramAgent); Whitebox.setInternalState(cliUnderTest, "yarnClient", yarnClient); Whitebox.setInternalState(cliUnderTest, "consolePresent", true); suppress(constructor(WebServicesClient.class, new Class[0])); List<ApplicationReport> runningApplications = new ArrayList<>(); ApplicationReport app1 = mockRunningApplicationReport("application-id-1", "app1"); ApplicationReport app2 = mockRunningApplicationReport("application-id-2", "app2"); runningApplications.add(app1); runningApplications.add(app2); when(yarnClient.getApplications(Mockito.any(Set.class))).thenReturn(runningApplications); when(stramAgent.issueStramWebRequest( Mockito.any(WebServicesClient.class), Mockito.anyString(), Mockito.any(StramAgent.StramUriSpec.class), Mockito.any(WebServicesClient.WebServicesHandler.class))) .thenReturn(new JSONObject()); final ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); final ByteArrayOutputStream stdErr = new ByteArrayOutputStream(); PrintStream beforeOut = System.out; PrintStream beforeErr = System.err; System.setOut(new PrintStream(stdOut, true)); System.setErr(new PrintStream(stdErr, true)); String shutdownAppsCommand = "shutdown-app app1 notExisting app2"; cliUnderTest.processLine(shutdownAppsCommand, new ConsoleReader(), true); Assert.assertEquals( "Shutdown of app application-id-1 requested: {}\nShutdown of app application-id-2 requested: {}\n", stdOut.toString() ); Assert.assertEquals( "Failed to request shutdown for app notExisting: Application with id or name notExisting not found\n", stdErr.toString() ); System.setOut(beforeOut); System.setErr(beforeErr); }