@Test
	public void testJobFailed() throws InterruptedException, ExecutionException, TimeoutException {
		JobInstance jobInstance = new JobInstance(3l, "masterFoo");
		JobExecution jobExecution = new JobExecution(5l);
		jobExecution.setJobInstance(jobInstance);
		StepExecution stepExecution = new StepExecution("masterFoo", jobExecution, 7l);
		final StepContribution stepContribution = new StepContribution(stepExecution);
		final ChunkContext chunkContext = new ChunkContext(new StepContext(stepExecution));

		JobDefinition jobDefinition = new JobDefinition("foo", "foo");
		when(jobDefinitionRepository.findOne("foo")).thenReturn(jobDefinition);
		when(instanceRepository.findOne("foo")).thenReturn(jobDefinition);

		FutureTask<RepeatStatus> result = new FutureTask<RepeatStatus>(new Callable<RepeatStatus>() {

			@Override
			public RepeatStatus call() throws Exception {
				return tasklet.execute(stepContribution, chunkContext);
			}
		});

		taskExecutor.execute(result);

		JobParameters jobParameters = new JobParametersBuilder().addString(JobLaunchingTasklet.XD_ORCHESTRATION_ID,
				"3").toJobParameters();
		JobExecution slaveExecution = new JobExecution(9l, jobParameters);
		slaveExecution.setStatus(BatchStatus.FAILED);
		slaveExecution.setExitStatus(new ExitStatus("Job Failure"));
		slaveExecution.setEndTime(new Date());

		//This is to allow the job to "start" before mocking the sending of the event
		Thread.sleep(1000l);

		this.listeningChannel.send(MessageBuilder.withPayload(slaveExecution).build());

		try {
			assertEquals(result.get(5, TimeUnit.SECONDS), RepeatStatus.FINISHED);
		}
		catch (ExecutionException e) {
			Throwable cause = e.getCause();
			assertEquals(cause.getClass(), UnexpectedJobExecutionException.class);
			assertEquals(cause.getMessage(), "Step failure: foo failed.");
		}

		assertEquals(stepExecution.getExecutionContext().get(JobLaunchingTasklet.XD_ORCHESTRATION_ID), "3");

		verify(bus).bindPubSubConsumer(eq("tap:job:foo.job"), any(PublishSubscribeAmqpChannel.class),
				(Properties) isNull());
	}
@author Michael Minella
@author Gary Russell
spring-xd-dirt/src/test/java/org/springframework/xd/dirt/batch/tasklet/JobLaunchingTaskletTests.java