【问题标题】:Executing a Spring Batch job incrementer when running through JUnit通过 JUnit 运行时执行 Spring Batch 作业增量器
【发布时间】:2012-12-17 21:56:27
【问题描述】:

我有一个带有参数的 Spring Batch 作业,并且每次运行作业时参数通常都是相同的。默认情况下,Spring Batch 不允许您重复使用相同的参数......所以我创建了一个简单的增量器并将其添加到我的工作中,如下所示:

http://numberformat.wordpress.com/2010/02/07/multiple-batch-runs-with-spring-batch/

当使用标准 CommandLineJobRunner 运行我的作业时,我必须传递 -next 参数才能使用我的增量器。

但是,当我从 JUnit 类中运行端到端作业测试时,使用 JobLauncherTestUtils.launchJob( JobParameters )... 我找不到声明应该使用我的增量器的方法。该作业只是悄悄地跳过,大概是因为它已经使用这些参数运行(请参阅下面的注释)。

JobParameters 类用于保存名称-值对的集合...但 -next 参数不同。它以破折号开头,没有对应的值。我尝试了各种实验,但尝试向JobParameters 集合添加一些东西似乎不是问题。

有谁知道相当于将-next 传递给CommandLineJobRunner 的JUnit?

注意:我认为问题是我的增量器被忽略了,因为:

  1. 作业第一次运行,如果我清除作业存储库数据库,它就会运行。它只会在重试时失败。
  2. 当我对变量进行硬编码并完全删除参数时,该作业运行良好,重试等等。

【问题讨论】:

    标签: java spring batch-processing spring-batch


    【解决方案1】:

    JobLauncherTestUtils 类包含一个 getUniqueJobParameters 方法,它可以满足完全相同的需求。

    /**
    * @return a new JobParameters object containing only a parameter for the
    * current timestamp, to ensure that the job instance will be unique.
    */
    public JobParameters getUniqueJobParameters() {Map<String, JobParameter> 
    parameters = new HashMap<String, JobParameter>();
    parameters.put("random", new JobParameter((long) (Math.random() * JOB_PARAMETER_MAXIMUM)));
    return new JobParameters(parameters);
    }
    

    示例用法是,

    JobParameters params = new JobParametersBuilder (jobLauncherTestUtils.getUniqueJobParameters()).toJobParameters();
         //extra parameters to be added
    JobExecution jobExecution = jobLauncherTestUtils.launchJob(params);
    

    【讨论】:

    • 嗯...所以单元测试并没有真正测试作业的迭代器,而是完全绕过了对它的需求。到目前为止,关于 Spring Batch 的很多事情对我来说都是不直观的......但是这种方法确实解决了我的问题,所以谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 2014-10-15
    相关资源
    最近更新 更多