0 Replies Latest reply on Apr 4, 2017 11:48 AM by Cheng Fang

    The easy-to-use item writer mockItemWriter

    Cheng Fang Master

      Item writer is a required artifact in a chunk-type step.  Sometimes when we are experimenting or testing a batch job, we just want to quickly see and verify the sample data from the item writer.  Probably we just need this item writer to log items to the screen for visual checking, or save to a tmp file, or even save the data to some class fields that can be easily verified by a test.

       

      What I've done in the past in such situation, is to use csvItemWriter to write to a temp file, then load the file into memory for verification.  This seems a waste and unnecessarily complicates the test code.

       

      Now introduce org.jberet.support.io.MockItemWriter, the easy-to-use item writer for easy testing and prototyping.  With easy configuration, it can

       

      • log data items to console
      • save data items to a file as string representation
      • save data items to a class field
      • do nothing

       

      For example,

       

      <writer ref="mockItemWriter">
      </writer>

      The above example logs data items to console.

       

       

      <writer ref="mockItemWriter">
        <properties>
         <property name="toConsole" value="true"/>
        </properties>
      </writer>

      The above example logs data items to console.

       

       

      <writer ref="mockItemWriter">
        <properties>
         <!-- Name of a class with a public static java.util.List field to hold data items -->
        <property name="toClass" value="org.jberet.support.io.MockItemWriterTest$DataHolder"/>
        </properties>
      </writer>

       

      public static final class DataHolder {

         public static final List data = new ArrayList();
      }

      The above example saves data items to a public static java.util.List field in class DataHolder.

       

       

      <writer ref="mockItemWriter">
        <properties>
        <property name="toFile" value="/tmp/xxx.txt"/>
        <property name="writeMode" value="overwrite"/>
        </properties>
      </writer>

      The above example saves data items to file /tmp/xxx.txt with overwrite mode.  The default write mode is append.

       

       

      <writer ref="mockItemWriter">
        <properties>
        <property name="toConsole" value="false"/>
        </properties>
      </writer>

      The above example does nothing, since logging to console is turned off.

       

       

      <writer ref="mockItemWriter">
        <properties>
        <property name="toConsole" value="true"/>
        <property name="toClass" value="org.jberet.support.io.MockItemWriterTest$DataHolder"/>
        </properties>
      </writer>

      The above example logs data items to console, and saves to a public static List field of class DataHolder.

       

       

      For more info, see

      JBERET-322

      Implement MockItemWriter that writes to console, file, or class field

       

      jsr352/MockItemWriterTest.java at master · jberet/jsr352 · GitHub

       

      jsr352/org.jberet.support.io.MockItemWriterTest.xml at master · jberet/jsr352 · GitHub