title: Spring Batch JUnit test for multiple jobs
body: <p>I am having two jobs configured in one context file</p>
  
  <pre><code>&lt;batch:job id="JobA" restartable="true"&gt;
          &lt;batch:step id="abc"&gt;
              &lt;batch:tasklet &gt;
                  &lt;batch:chunk reader="reader" writer="writer" processor="processor"  /&gt;
              &lt;/batch:tasklet&gt;
        &lt;/batch:step&gt;
  
      &lt;/batch:job&gt;
  
  &lt;batch:job id="JobB" restartable="true"&gt;
          &lt;batch:step id="abc"&gt;
              &lt;batch:tasklet &gt;
                  &lt;batch:chunk reader="reader" writer="writer" processor="processor"  /&gt;
              &lt;/batch:tasklet&gt;
        &lt;/batch:step&gt;
  
      &lt;/batch:job&gt;
  </code></pre>
  
  <p>When i am doing unit testing for the JobA using <code>JobLauncherTestUtils</code> and testing the job launch it is throwing an exception saying </p>
  
  <pre><code>No unique bean of type [org.springframework.batch.core.Job;] is defined: expected single matching bean but found 2: [JobA, JobB]
  </code></pre>
  
  <p>i tried using <code>@Qualifier</code> for autowire still the same thing. Where am i doing wrong here</p>
  
  <p>edited</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath:META-INF/spring/batch-test-context.xml" })
  public class TestJob {
  
      @Autowired
      private JobExplorer jobExplorer;
  
      @Autowired
      @Qualifier("JobA")
      private Job JobA;
  
  
      @Autowired
      private JobLauncherTestUtils jobLauncherTestUtils;
  
  
      @Test
      public void testJob() throws Exception {
          JobParameters jobParameters = getNextJobParameters(getJobParameters());
          assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.getJobLauncher().run(JobA, jobParameters));
      }
  
  
      private JobParameters getJobParameters() {
          JobParametersBuilder jobParameters = new JobParametersBuilder();
          jobParameters.addString("param", "123");
          return jobParameters.toJobParameters();
      }
  
  
      private JobParameters getNextJobParameters(JobParameters jobParameters) {
          String jobIdentifier = jobLauncherTestUtils.getJob().getName();
          List&lt;JobInstance&gt; lastInstances = jobExplorer.getJobInstances(jobIdentifier, 0, 1);
          JobParametersIncrementer incrementer = jobLauncherTestUtils.getJob().getJobParametersIncrementer();
          if (lastInstances.isEmpty()) {
              return incrementer.getNext(jobParameters);
          } else {
              List&lt;JobExecution&gt; lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0));
              return incrementer.getNext(lastExecutions.get(0).getJobParameters());
          }
      }
  }
  </code></pre>
  
  <p>exception was   </p>
  
  <pre><code>No unique bean of type [org.springframework.batch.core.Job;] is defined: expected single matching bean but found 2: [JobA, JobB]`
  </code></pre>