Using @SpringApplicationConfiguration: How to set jobparameters in tests when using spring-batch and spring-boot

大憨熊 提交于 2020-01-15 06:56:27

问题


Is there a way, how I can define jobparameters in integration test, when using pure spring-boot with spring-batch?

When I define a simple Batch-Job in Spring-Boot and start it with SpringApplication.run(args), I can pass my program-arguments in the run method.

Since I have Spring-Boot-Batch activated, those arguments are converted into JobParameters and are then passed to the job. This happens inside the class JobLauncherCommandLineRunner.

Afterwards, I can read this jobparameters via the jobexecution object.

Now, I would like to write a test, which starts a job almost the same way, as it will be in production. Therefore, I'm using @SpringApplicationConfiguration and write a test like the following one:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {MyBatchRootConfiguration.class})
@IntegrationTest({"aProp=aValue"})
public class MyBatchTestClass {
   @Test
   public void launchTest() {
   }
}

Note: MyBatchRootConfiguration defines the job and also adds the @SpringBootApplication, @EnableBatchProcessing and so on.

The problem I'm facing now: how can I pass arguments that will be converted to jobparameters? Is there a way, how this could be done?

As far as I have seen, the @SpringApplicationConfiguration defines the loader as "SpringApplicationContextLoader". Therefore, adding @SpringApplicationConfiguration to a test class eventually calls the method SpringApplicationContextLoader.loadContext(...). In this method, the SpringContext is created and an instance of SpringApplication is also created. Finally, in the following line of SpringApplicationContextLoader.loadContext() (line 103 for version 1.2.4-RELEASE of spring-boot)

ConfigurableApplicationContext applicationContext = application.run();

run is called.

But contrary to the example of spring-boot-batch on https://spring.io/guides/gs/batch-processing/

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

which in the end also calls the non-static run method of SpringApplication, there is no way to pass or to define args.

But this is crucial for spring-batch, since these argsare converted to JobParameters in JobLauncherCommandLineRunner.

(SpringApplication.run(String ... args) calls SpringAppplication.afterRefresh(..., String[] args)calls SpringApplication.runCommandLineRunners(..., String[] args)calls JobLauncherCommandLineRunner.run(String ... args))

I guess there is no other way than adapting the JobLauncherCommandLinerRunner, or using it directly as in the following example:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {MyBatchRootConfiguration.class})
@IntegrationTest({"aProp=aValue"})
public class MyBatchTestClass {

    @Autowired
    private JobLauncherCommandLineRunner runner;

   @Test
   public void launchTest() {
        String[] myArgs = new String[]{"jobParam1=value1"};
        runner.run(myArgs);
   }
}

I know, that I can use the JobLauncherTestUtils, but what if I would like to have a test, that starts a job "the spring-boot way"? @SpringApplicationConfiguration is exactly for that.

But if we liked to use it to launch a Spring-Batch job, it looks to me as if we are missing a way to define the program-arguments and therefore to define jobparameters.

This leads me to the conclusion, that you cannot use @SpringApplicationConfiguration to test a job, if you have a job that depends on jobparameters.

Are there any ideas or solutions?

Thanks

Hansjoerg

来源:https://stackoverflow.com/questions/31181947/using-springapplicationconfiguration-how-to-set-jobparameters-in-tests-when-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!