Reputation:
I am newbie to the Spring Batch, I have a following main program which I want to convert it into the test case using jobLauncherTestUtils
. How we can do that?
I followed http://docs.spring.io/spring-batch/reference/html/testing.html, but I don't see any pointers. Please guide.
private void run() {
String[] springConfig = { "spring/batch/jobs/job-extract-users.xml" };
ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("testActualJob");
try {
JobParameters param = new JobParametersBuilder().addString("age", "20").toJobParameters();
JobExecution execution = jobLauncher.run(job, param);
System.out.println("----------------------------------------------");
System.out.println("Exit Status : " + execution.getStatus());
System.out.println("Exit Status : " + execution.getAllFailureExceptions());
System.out.println("-----------------------------------------------");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done !!");
}
Upvotes: 2
Views: 14165
Reputation: 1467
just posting another approach for Junit -
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:request-jobs/spring-batch-config-jobs.xml"})
public class TestMyJob {
@Autowired
private JobLauncher jobLauncher;
@Autowired
@Qualifier(value = "myJobName")
private Job myJob;
@Test
public void testJob() throws Exception {
JobParametersBuilder builder = new JobParametersBuilder()
.addLong("timestamp", System.currentTimeMillis());
JobExecution jobExecution = jobLauncher.run(myJob, builder.toJobParameters());
Assert.assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode());
}
}
Upvotes: 0
Reputation: 6477
In case you want to run only a specific step
that has job parameters, you can do it like the following:
JobParameters param = new JobParametersBuilder()
.addString("language", "en_US")
.toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchStep("stepName", param);
Upvotes: 4
Reputation: 5649
Quoted from the documentation:-
The launchJob() method is provided by the JobLauncherTestUtils class. Also provided by the utils class is launchJob(JobParameters), which allows the test to give particular parameters.
Your code will look like below:-
JobParameters param = new JobParametersBuilder().addString("age", "20").toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchJob(param).getStatus();
Upvotes: 5
Reputation:
I find the solution here:http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/test/JobLauncherTestUtils.html
@Test
public void testMysqlToXMLWithParameters() throws Exception {
JobParameters jobParameters = new JobParametersBuilder().addString("age", "20").toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobParameters);
Assert.assertEquals(jobExecution.getStatus(), BatchStatus.COMPLETED);
}
Done !
Upvotes: 2