spring-projects / spring-batch

Spring Batch is a framework for writing batch applications using Java and Spring
http://projects.spring.io/spring-batch/
Apache License 2.0
2.69k stars 2.33k forks source link

SimpleJobLauncher not using jobIncrementer #4397

Open songmag opened 1 year ago

songmag commented 1 year ago

Please do a quick search on Github issues first, there might be already a duplicate issue for the one you are about to create. If the bug is trivial, just go ahead and create the issue. Otherwise, please take a few moments and fill in the following sections:

Bug description A clear and concise description of what the bug is about.

Simple Job Launcher not using JobParametersIncrementer

i want same result using that cli run but the test util is using the simple job launcher

so, i want to change the simpleJobLauncher

image

Environment Please provide as many details as possible: Spring Batch version, Java version, which database you use if any, etc spring batch 4.3 java 11

Steps to reproduce Steps to reproduce the issue.

Expected behavior A clear and concise description of what you expected to happen.

        jobLauncherTestUtils.setJob(job);
        var result = jobLauncherTestUtils.launchJob();
        Assertions.assertThat(result.getJobParameters().getString("year")).isEqualTo(DateTimeFormatter.ofPattern("yyyyMM").format(LocalDateTime.now()));
        Assertions.assertThat(result.getStatus()).isEqualTo(BatchStatus.COMPLETED);

Minimal Complete Reproducible example Please provide a failing test or a minimal complete verifiable example that reproduces the issue. Bug reports that are reproducible will take priority in resolution over reports that are not reproducible.

        jobLauncherTestUtils.setJob(job);
        var result = jobLauncherTestUtils.launchJob(job.getJobParametersIncrementer().getNext(jobLauncherTestUtils.getUniqueJobParameters()));
        Assertions.assertThat(result.getJobParameters().getString("year")).isEqualTo(DateTimeFormatter.ofPattern("yyyyMM").format(LocalDateTime.now()));
        Assertions.assertThat(result.getStatus()).isEqualTo(BatchStatus.COMPLETED);
songmag commented 1 year ago

I'm sorry. In the case of SimpleJobLauncher, it is thought that it does not depend on parameters. So, if modification is needed, it seems that JobLauncherTestUtils needs to be modified. Since 5.0.0, the spring launcher test utility seems to be deprecated. What should I do if I am using version 4.3?

songmag commented 1 year ago

JobLauncherTestUtils

How about this modification in JobLauncherTestUtils?

    public JobExecution launchJob() throws Exception {
        JobParametersIncrementer incrementer = this.job.getJobParametersIncrementer();
        if (incrementer == null) {
            return this.launchJob(this.getUniqueJobParameters());
        }
        return this.launchJob(incrementer.getNext(this.getUniqueJobParameters()));
    }
fmbenhassine commented 1 year ago

i want same result using that cli run but the test util is using the simple job launcher

Which cli are you referring to? The CommandLineJobRunner does not use the incrementer unless you specify the -next option. The equivalent API is JobOperator#startNextInstance. SimpleJobLauncher is not designed to start the next instance with the run method since the user gives the parameters and can increment them beforehand if needed.

So, if modification is needed, it seems that JobLauncherTestUtils needs to be modified. Since 5.0.0, the spring launcher test utility seems to be deprecated.

Which deprecated test utility you are referring to?

Please clarify the bug report. I still don't fully understand the issue your are reporting. Thank you upfront.