gpt4 book ai didi

java - 如何将 JobParameters 传递给 Spring Batch Junit 测试用例 JobLauncherTestUtils?

转载 作者:行者123 更新时间:2023-12-02 09:43:46 42 4
gpt4 key购买 nike

我是 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 !!");
}

最佳答案

引自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();

关于java - 如何将 JobParameters 传递给 Spring Batch Junit 测试用例 JobLauncherTestUtils?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41804389/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com