title: How to mock custom JdbcCursorItemReader during individual step unit testing on Spring Batch?
body: <p>I am trying to create a Junit test case for testing an individual step of my batch Job which in turn has a custom <code>JdbcCursorItemReader</code> and writes data into Flat file. I am unable to mock the data that can be injected into the processor for writing it into the Flatfile. Please help.</p>
  
  <p>I followed steps in below link to create a test case to launch an individual step:</p>
  
  <p><a href="https://docs.spring.io/spring-batch/trunk/reference/html/testing.html" rel="nofollow noreferrer">https://docs.spring.io/spring-batch/trunk/reference/html/testing.html</a></p>
  
  <p>Since my reader component is stepscope I am using the context around section 10.4 in above link to put the required domain objects into <code>ExecutionContext</code> but while launching the step it does not recognize the same instead it executes the actual SQL query which I am injecting through config xml.</p>
  
  <p>My config xml has step as below :</p>
  
  <pre><code>&lt;step id="sample-step" next="next-step"&gt;
   &lt;tasklet&gt;
      &lt;chunk reader="customJDBCReader" processor="customProcessor" 
       writer="customFlatFileItemWriter" commit-interval="XXX" /&gt;
   &lt;/tasklet&gt;
  &lt;/step&gt;
  </code></pre>
  
  <p>My customreader implements <code>JdbcCursorItemReader</code> which has a SQL defined for reading information from database.</p>
  
  <p>During my unit testing I am trying to mock the data instead of depending on actual data from database</p>
  
  <pre><code>private StepExecution execution;
  
  @BeforeStep
  public StepExecution getStepExection() {
      execution = MetaDataInstanceFactory.createStepExecution();
      return execution;
  }
  
  @Test
  public void testSampleStep() throws UnexpectedInputException, ParseException, Exception {
      MyDomainObject domainObject= new MyDomainObject ();
      domainObject.setID("sampleID");
      domainObject.setName("sampleName");
      domainObject.setImage("sampleImage");
      execution.getExecutionContext().put("someKey", domainObject);
      execution.setReadCount(1);
  
      JobExecution jobExecution = jobLauncherTestUtils.launchStep("sample-step", jobParameters, execution.getExecutionContext());
      AssertFile.assertFileEquals(new FileSystemResource(EXPECTED_FILE), new FileSystemResource(OUTPUT_FILE));
  }
  </code></pre>
  
  <p>The expected result should be the launchstep needs to get the data from <code>executionContext</code> instead of database to write the same into faltfile.</p>