使用SpringJUnit4ClassRunner运行时不执行Spring批处理步骤

时间:2016-09-02 07:09:25

标签: java spring spring-batch junit4 spring-test

我正在尝试使用JUnit测试我的spring批处理应用程序。 我已经设置了项目,并且在使用普通的java main方法运行时工作正常。但是当我尝试编写JUnit测试并尝试使用SpringJUnit4ClassRunner执行时,它没有执行属于Job的步骤。以下是我写的JUnit测试用例。

@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();
    }
}

以下代码段显示了作业配置

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

有人可以通过SpringJUnit4ClassRunner来了解正在执行的工作吗?

0 个答案:

没有答案