Using @SpringApplicationConfiguration: how to set work parameters in tests when using spring -batch and spring -boot

Is there a way how I can determine the parameters in the 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 programmatic arguments in the run method.

Since I activated spring-Boot-Batch, these arguments are converted to JobParameters and then passed to the job. This happens inside the JobLauncherCommandLineRunner class.

Then I can read these job parameters using the jobexecution object.

Now I would like to write a test that starts the job in much the same way as in the production process. So I use @SpringApplicationConfiguration and write a test similar to the following:

 @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 @SpringBootApplication , @EnableBatchProcessing , etc.

The problem I am facing now: how to pass arguments that will be converted to jobparameters? Is there any way how this can be done?

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

 ConfigurableApplicationContext applicationContext = application.run(); 
Called

run.

But, contrary to the example of spring-boot-batch at 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 at the end also calls the SpringApplication non-static start method, it is not possible to pass or define args .

But this is important for spring-batch, as these args 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 think there is no other way than adapting 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 JobLauncherTestUtils , but what if I wanted to have a test that runs "w760> -boot"? @SpringApplicationConfiguration exactly for this.

But if we liked to use it to run the spring -Batch task, it seems to me that we lack a way to determine the arguments of the program and, therefore, determine the parameters of work.

This leads me to the conclusion that you cannot use @SpringApplicationConfiguration to validate a job if you have work that depends on the working parameters.

Any ideas or solutions?

thanks

Hansjorg

+2
source share

All Articles