javaspring-batchspring-batch-admin

How to pass JobParameters to the Spring Batch Junit Test case JobLauncherTestUtils?


I am newbie to the Spring Batch, I have a following main program which I want to convert it into the test case using jobLauncherTestUtils. How we can do that?

I followed http://docs.spring.io/spring-batch/reference/html/testing.html, but I don't see any pointers. Please guide.

private void run() {
        String[] springConfig = { "spring/batch/jobs/job-extract-users.xml" };
        ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);

        JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
        Job job = (Job) context.getBean("testActualJob");

        try {
            JobParameters param = new JobParametersBuilder().addString("age", "20").toJobParameters();

            JobExecution execution = jobLauncher.run(job, param);
            System.out.println("----------------------------------------------");
            System.out.println("Exit Status : " + execution.getStatus());
            System.out.println("Exit Status : " + execution.getAllFailureExceptions());
            System.out.println("-----------------------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Done !!");
    } 

Solution

  • I find the solution here:http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/test/JobLauncherTestUtils.html

    @Test
        public void testMysqlToXMLWithParameters() throws Exception {
            JobParameters jobParameters = new JobParametersBuilder().addString("age", "20").toJobParameters();
            JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobParameters);
            Assert.assertEquals(jobExecution.getStatus(), BatchStatus.COMPLETED);
        }
    

    Done !