如何将 JobParameters 传递给 Spring Batch Junit 测试用例 JobLauncherTestUtils?
How to pass JobParameters to the Spring Batch Junit Test case JobLauncherTestUtils?
我是 Spring 批处理的新手,我有以下主程序,我想使用 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 !!");
}
我在这里找到解决方案: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);
}
完成!
转自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.
您的代码如下所示:-
JobParameters param = new JobParametersBuilder().addString("age", "20").toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchJob(param).getStatus();
如果您只想 运行 具有作业参数的特定 step
,您可以按以下方式进行操作:
JobParameters param = new JobParametersBuilder()
.addString("language", "en_US")
.toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchStep("stepName", param);
只是为 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());
}
}
我是 Spring 批处理的新手,我有以下主程序,我想使用 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 !!");
}
我在这里找到解决方案: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);
}
完成!
转自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.
您的代码如下所示:-
JobParameters param = new JobParametersBuilder().addString("age", "20").toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchJob(param).getStatus();
如果您只想 运行 具有作业参数的特定 step
,您可以按以下方式进行操作:
JobParameters param = new JobParametersBuilder()
.addString("language", "en_US")
.toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchStep("stepName", param);
只是为 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());
}
}