我是Spring Batch的新手,我有一个以下主程序,我希望使用jobLauncherTestUtils
将其转换为测试用例。我们怎么做到这一点?
我跟着http://docs.spring.io/spring-batch/reference/html/testing.html,但我没有看到任何指针。请指导。
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 !!");
}
答案 0 :(得分:5)
引自documentation: -
startupJob发放方法由JobLauncherTestUtils类提供。 utils类还提供了 launchJob(JobParameters),其中 允许测试给出特定的参数。
您的代码如下所示: -
JobParameters param = new JobParametersBuilder().addString("age", "20").toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchJob(param).getStatus();
答案 1 :(得分:3)
如果您只想运行具有作业参数的特定step
,可以执行以下操作:
JobParameters param = new JobParametersBuilder()
.addString("language", "en_US")
.toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchStep("stepName", param);
答案 2 :(得分:2)
我在这里找到解决方案: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);
}
完成!
答案 3 :(得分:0)
仅发布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());
}
}