title: Using @SpringApplicationConfiguration: How to set jobparameters in tests when using spring-batch and spring-boot
body: <p>Is there a way, how I can define jobparameters in integration test, when using pure spring-boot with spring-batch?</p>
  
  <p>When I define a simple Batch-Job in Spring-Boot and start it with <code>SpringApplication.run(args)</code>, I can pass my program-arguments in the run method.</p>
  
  <p>Since I have Spring-Boot-Batch activated, those arguments are converted into <code>JobParameters</code> and are then passed to the job. This happens inside the class <code>JobLauncherCommandLineRunner</code>.</p>
  
  <p>Afterwards, I can read this jobparameters via the jobexecution object.</p>
  
  <p>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 <code>@SpringApplicationConfiguration</code> and write a test like the following one:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = {MyBatchRootConfiguration.class})
  @IntegrationTest({"aProp=aValue"})
  public class MyBatchTestClass {
     @Test
     public void launchTest() {
     }
  }
  </code></pre>
  
  <p>Note: <code>MyBatchRootConfiguration</code> defines the job and also adds the <code>@SpringBootApplication</code>, <code>@EnableBatchProcessing</code> and so on.</p>
  
  <p>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?</p>
  
  <p>As far as I have seen, the <code>@SpringApplicationConfiguration</code> defines the loader as "SpringApplicationContextLoader". Therefore, adding <code>@SpringApplicationConfiguration</code> to a test class eventually calls the method <code>SpringApplicationContextLoader.loadContext(...)</code>. In this method, the SpringContext is created and an instance of <code>SpringApplication</code> is also created. Finally, in the following line of <code>SpringApplicationContextLoader.loadContext()</code> (line 103 for version 1.2.4-RELEASE of spring-boot)</p>
  
  <pre><code>ConfigurableApplicationContext applicationContext = application.run();
  </code></pre>
  
  <p>run is called.</p>
  
  <p>But contrary to the example of spring-boot-batch on <a href="https://spring.io/guides/gs/batch-processing/" rel="nofollow">https://spring.io/guides/gs/batch-processing/</a></p>
  
  <pre><code>@SpringBootApplication
  public class Application {
  
      public static void main(String[] args) throws Exception {
          SpringApplication.run(Application.class, args);
      }
  }
  </code></pre>
  
  <p>which in the end also calls the non-static run method of SpringApplication, there is no way to pass or to define <code>args</code>.</p>
  
  <p>But this is crucial for spring-batch, since these <code>args</code>are converted to <code>JobParameters</code> in <code>JobLauncherCommandLineRunner</code>.</p>
  
  <p>(<code>SpringApplication.run(String ... args)</code> calls <code>SpringAppplication.afterRefresh(..., String[] args)</code>calls <code>SpringApplication.runCommandLineRunners(..., String[] args)</code>calls <code>JobLauncherCommandLineRunner.run(String ... args)</code>)</p>
  
  <p>I guess there is no other way than adapting the JobLauncherCommandLinerRunner, or using it directly as in the following example:</p>
  
  <pre><code>@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);
     }
  }
  </code></pre>
  
  <p>I know, that I can use the <code>JobLauncherTestUtils</code>, but what if I would like to have a test, that starts a job "the spring-boot way"? <code>@SpringApplicationConfiguration</code> is exactly for that. </p>
  
  <p>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.</p>
  
  <p>This leads me to the conclusion, that you cannot use <code>@SpringApplicationConfiguration</code> to test a job, if you have a job that depends on jobparameters.</p>
  
  <p>Are there any ideas or solutions?</p>
  
  <p>Thanks</p>
  
  <p>Hansjoerg</p>