public void testAsyncStopOfStartingJob() throws Exception {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("org/springframework/batch/core/repository/dao/OptimisticLockingFailureTests-context.xml");
Job job = applicationContext.getBean(Job.class);
JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
JobOperator jobOperator = applicationContext.getBean(JobOperator.class);
JobExecution jobExecution = jobLauncher.run(job, new JobParametersBuilder()
.addLong("test", 1L)
.toJobParameters());
Thread.sleep(1000);
jobOperator.stop(jobExecution.getId());
while(jobExecution.isRunning()) {
// wait for async launched job to complete execution
}