1

I am trying to test my spring batch application with JUnit. I have setup the project and it is working fine when running using the normal java main method. But when i try to write JUnit test and trying to execute using SpringJUnit4ClassRunner, it is not executing the step that belongs to the Job. Following is the JUnit test case i have written.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CASConfig.class)
public class DailyDataPreparationStepTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(DailyDataPreparationStepTest.class);

    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private Environment environment;
    @Autowired
    private JobLauncher jobLauncher;
    @Autowired
    private JobRepository jobRepository;
    @Autowired
    private Job dailyDataPrepJob;
    @Autowired
    private DailyDataPreparationStep dailyDataPreparationStep;

    //
    private JobLauncherTestUtils jobLauncherTestUtils;

    static{
        System.setProperty("env", "test");
    }


    @Before
    public void setUp() throws Exception {
        jobLauncherTestUtils = new JobLauncherTestUtils();
        jobLauncherTestUtils.setJobLauncher(jobLauncher);
        jobLauncherTestUtils.setJobRepository(jobRepository);
        jobLauncherTestUtils.setJob(dailyDataPrepJob);
    }

    @After
    public void tearDown() throws Exception {

    }

    @Test
    public void testJobExecution(){
        try {
            jobLauncherTestUtils.launchJob(this.jobParameters("1", "System", DateUtil.getCurrentDateInYYYYMMDD(), "log.log"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private JobParameters jobParameters(String jobId, String userId, String executionDate, String jobLogFileName){
        return new JobParametersBuilder()
                .addString(Constants.BatchJobParameter.PARAM_EXECUTION_DATE, executionDate)
                .addString(Constants.BatchJobParameter.PARAM_JOBID, jobId)
                .addString(Constants.BatchJobParameter.PARAM_JOBLOGFILENAME, jobLogFileName)
                .addString(Constants.BatchJobParameter.PARAM_USERID, userId)
                .addDate(Constants.BatchJobParameter.PARAM_TIMESTAMP, Calendar.getInstance().getTime())
                .toJobParameters();
    }
}

Following code snipt shows the Job configuration

@Bean(name = "dailyDataPrepJob")
public Job dailyDataPreparationJob() throws Exception{
    return jobBuilderFactory.get("dailyDataPrepJob")
            .start(dailyDataPrepStep)
            .incrementer(new RunIdIncrementer())
            .listener(jobExecutionListener)
            .build();
}

Can anyone give some idea what's going on while executing the job via SpringJUnit4ClassRunner?

Mayuran
  • 669
  • 2
  • 8
  • 39
  • Hi Mayuran, are you sure you have injected correct configurations for the test context? (i .e in CASConfig.class). Also since you haven't share the implementation of launchJob., I hope that contains the code to trigger the job with your job parameters like this JobExecution execution = jobLauncher.run(job, new JobParameters(jobParams)); – Tharsan Sivakumar Sep 02 '16 at 09:44
  • 1
    @TharsanSivakumar, Nice to see you here.:D. I don't have specific configuration for test context. Same context is being used. Also i have managed to find the issue. It is not related to the spring setup, JUnit is having issue when executing multiple threaded application. As i am using separate TaskExecutor, all jobs are executed in a separate thread pool. If you want more info, Please refer to the following link. http://stackoverflow.com/questions/2836885/junit-terminates-child-threads – Mayuran Sep 02 '16 at 10:15
  • What is happening? What do the logs say? Are any steps being executed? – Michael Minella Sep 06 '16 at 19:34

0 Answers0