title: How to mock writer in Spring Batch Junit tests?
body: <p>I am testing Spring Batch application using Junit:</p>
  
  <p>This is my job xml configuration:</p>
  
  <pre><code>    &lt;!-- parse queue --&gt;
      &lt;batch:step id="parseQueue"&gt;
  
          &lt;batch:tasklet&gt;
              &lt;batch:chunk
  
              reader="readQueue"
              processor="processQueue"
              writer="customItemWriter"
              commit-interval="100"&gt;
  
              &lt;/batch:chunk&gt;
          &lt;/batch:tasklet&gt;
  
      &lt;/batch:step&gt;
  </code></pre>
  
  <p>I am testing the "parseQueue" step.</p>
  
  <p>I am using JobLauncherTestUtils to test a step like below:</p>
  
  <p>jobLauncherTestUtils.launchStep("parseQueue");</p>
  
  <p>The problem is it runs the entires step code. I mean reader, processor and writer.
  I want to skip the execution of Writer. </p>
  
  <p>Is there a way to do it?</p>
  
  <p>Any suggestion on mocking "Writer" in Spring Batch?</p>
  
  <p>I have tried to mock the Writer. It doesn't mock and the real Writer implementation gets called. Usually, it would have worked, but it just doesn't seem to be working with Spring Batch.</p>
  
  <p>Thanks in advance.</p>
  
  <p>Below is the code of my Test class:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {
          "classpath:/job/qhandler-job.xml",
          "classpath:/spring/qhandler-applicationContext.xml" })
  public class TestReader {
  
      @Autowired
      private JobLauncherTestUtils jobLauncherTestUtils;
  
      @Mock
      CustomItemWriter writer;
  
      @InjectMocks
      private ReadQueueMsgFromWs reader;
  
  
      @Before
      public void setup() {
          MockitoAnnotations.initMocks(this);
      }
  
      @Test
      public void testReader() throws UnexpectedInputException, ParseException, NonTransientResourceException, Exception
      {
          jobLauncherTestUtils.launchStep("parseQueue");
      }
  
  }
  </code></pre>