Unit Testing using JUnit for Spring Batch without XML configuration -


i new spring batch , started developping simple batch application. thinking of unit testing unsing junit healthy app , code ;)

the problem couldn't find ressource (examples, tutos ...) on internet shows how perform unit testing spring batch when using no xml.

here code more clear :

config class:

package my.company.project.name.batch.config @configuration @enablebatchprocessing @componentscan({         "my.company.project.name.batch.reader",         "my.company.project.name.batch.tasklet",         "my.company.project.name.batch.processor",         "my.company.project.name.batch.writer" }) @import({commonconfig.class}) public class myitembatchconfig {     @autowired     private stepbuilderfactory steps;      @autowired     private jobbuilderfactory jobbuilderfactory;      @autowired     private myitemtasklet myitemtasklet;      @bean     public job myitemjob(@qualifier("myitem") step loadproducts){         return jobbuilderfactory.get("myitemjob").start(mymethod).build();     }      @bean(name= "myitem")     public step mymethod(){         return steps.get("myitem").tasklet(myitemtasklet).build();     } } 

myitemreader class :

package my.company.project.name.batch.reader @component public class myitemreader implements itemreader<myitem>{      @value("${batch.load.produit.csv.file.path}")     private string csvfilepath;      private linkedlist<csvrawline> myitems;      @postconstruct     public void init() {         myitems = new linkedlist<>(csvutil.getcsvreader(myitem.class, csvfilepath));     }      @override     public myitem read() throws exception{         return myitems.poll();     } } 

itemprocessor class :

package my.company.project.name.batch.processor @component public class myitemprocessor implements itemprocessor<myitem, myitemprocessorresult> {     public myitemprocessorresult process(myitemitem) throws exception {      //processing business logic    } } 

itemwriter class :

package my.company.project.name.batch.writer @component public class myitemwriter implements itemwriter<myitem> {      @override     public void write(list<? extends myitem> myitems) throws exception {       //writer business logic     } } 

myitemtasklet class call previous classes in order achieve task wanted batch:

package package my.company.project.name.batch.tasklet @component public class myitembatchtasklet implements tasklet{      @autowired     public myitemreader myitemreader;      @autowired     public myitemprocessor myitemprocessor;      @autowired     public myitemewriter myitemwriter;      @override     public repeatstatus execute execute(stepcontribution contribution, chunkcontext chunkcontext) throws exception {        //calling myitemreader, myitemprocessor , myitemwriter business logic        return repeatstatus.finished     }  } 

myitemtasklet class launch tasklet main method :

package package my.company.project.name.batch public class myitemtaskletlauncher{     public myitemtaskletlauncher(){         //no implementation     }      public static void main (string[] args) throws ioexception, jobexecutionexception, namingexception {         launcher.launchwithconfig("launching myitemtasklet ...", myitembatchconfig.class,false);     } } 

i made simple batch application using spring batch , mybatis , junit.

the test codes of application runs unit testing without xml.

here test class job.

 @runwith(springjunit4classrunner.class) @springboottest(classes = {xxx.class, yyy.class, zzz.class, xxxjoblaunchertestutils.class}) public class jobtest {      @autowired     @qualifier(value = "xxxjoblaunchertestutils")     private joblaunchertestutils xxxjoblaunchertestutils;      @test     public void testxxxjob() throws exception {         jobexecution jobexecution = xxxjoblaunchertestutils.launchjob();         assertthat(jobexecution.getstatus(), is(batchstatus.completed));     } }  @component(value = "xxxjoblaunchertestutils") class xxxjoblaunchertestutils extends joblaunchertestutils {      @autowired     @qualifier(value = "xxxjob")     @override     public void setjob(job job) {         super.setjob(job);     } } 

about details, please see below link.

https://github.com/maeno/spring-batch-example/tree/master/src/test

i hope helpful.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -