Source:
@Test public void consoleShouldSuccessWithFollow() throws Exception { FreeStyleProject project = j.createFreeStyleProject("aProject"); if(Functions.isWindows()) { project.getBuildersList().add(new BatchFile("echo start - %BUILD_NUMBER%\r\n" + "ping 1.1.1.1 -n 1 -w 10000 >NUL\r\nver >NUL\r\necho after sleep - %BUILD_NUMBER%")); } else { project.getBuildersList().add(new Shell("echo start - ${BUILD_NUMBER}\nsleep 10s\n" + "echo after sleep - ${BUILD_NUMBER}")); } if (!project.scheduleBuild(0)) { fail("Job wasn't scheduled properly"); } while(!project.isBuilding()) { System.out.println("Waiting for build to start and sleep 1s..."); Thread.sleep(1000); } if(!project.getBuildByNumber(1).getLog().contains("start - 1")) { Thread.sleep(1000); } assertThat(project.getBuildByNumber(1).getLog(), containsString("start - 1")); assertThat(project.getBuildByNumber(1).getLog(), not(containsString("after sleep - 1"))); CLICommandInvoker.Result result = command .authorizedTo(Jenkins.READ, Job.READ, Item.BUILD) .invokeWithArgs("aProject", "1"); assertThat(result, succeeded()); assertThat(result.stdout(), containsString("start - 1")); assertThat(result.stdout(), not(containsString("after sleep - 1"))); result = command .authorizedTo(Jenkins.READ, Job.READ, Item.BUILD) .invokeWithArgs("aProject", "1", "-f"); assertThat(result, succeeded()); assertThat(result.stdout(), containsString("after sleep - 1")); assertThat(project.getBuildByNumber(1).isBuilding(), equalTo(false)); assertThat(project.getBuildByNumber(1).getResult(), equalTo(Result.SUCCESS)); assertThat(project.getBuildByNumber(1).getLog(), containsString("after sleep - 1")); }

Target:
@Test public void consoleShouldSuccessWithFollow() throws Exception { FreeStyleProject project = j.createFreeStyleProject("aProject"); if(Functions.isWindows()) { project.getBuildersList().add(new BatchFile("echo start - %BUILD_NUMBER%\r\n" + "ping -n 10 127.0.0.1 >nul\r\necho after sleep - %BUILD_NUMBER%")); } else { project.getBuildersList().add(new Shell("echo start - ${BUILD_NUMBER}\nsleep 10s\n" + "echo after sleep - ${BUILD_NUMBER}")); } if (!project.scheduleBuild(0)) { fail("Job wasn't scheduled properly"); } while(!project.isBuilding()) { System.out.println("Waiting for build to start and sleep 1s..."); Thread.sleep(1000); } if(!project.getBuildByNumber(1).getLog().contains("start - 1")) { Thread.sleep(1000); } assertThat(project.getBuildByNumber(1).getLog(), containsString("start - 1")); assertThat(project.getBuildByNumber(1).getLog(), not(containsString("after sleep - 1"))); CLICommandInvoker.Result result = command .authorizedTo(Jenkins.READ, Job.READ, Item.BUILD) .invokeWithArgs("aProject", "1"); assertThat(result, succeeded()); assertThat(result.stdout(), containsString("start - 1")); assertThat(result.stdout(), not(containsString("after sleep - 1"))); result = command .authorizedTo(Jenkins.READ, Job.READ, Item.BUILD) .invokeWithArgs("aProject", "1", "-f"); assertThat(result, succeeded()); assertThat(result.stdout(), containsString("after sleep - 1")); assertThat(project.getBuildByNumber(1).isBuilding(), equalTo(false)); assertThat(project.getBuildByNumber(1).getResult(), equalTo(Result.SUCCESS)); assertThat(project.getBuildByNumber(1).getLog(), containsString("after sleep - 1")); }