Java 使用SpringJUnit4ClassRunner运行时不执行Spring批处理步骤
我试图用JUnit测试我的spring批处理应用程序。 我已经设置了这个项目,当使用普通的java main方法运行时,它工作得很好。但是,当我尝试编写JUnit测试并尝试使用SpringJUnit4ClassRunner执行时,它并没有执行属于作业的步骤。下面是我编写的JUnit测试用例Java 使用SpringJUnit4ClassRunner运行时不执行Spring批处理步骤,java,spring,spring-batch,junit4,spring-test,Java,Spring,Spring Batch,Junit4,Spring Test,我试图用JUnit测试我的spring批处理应用程序。 我已经设置了这个项目,当使用普通的java main方法运行时,它工作得很好。但是,当我尝试编写JUnit测试并尝试使用SpringJUnit4ClassRunner执行时,它并没有执行属于作业的步骤。下面是我编写的JUnit测试用例 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = CASConfig.class) public class Da
@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();
}
}
下面的代码snipt显示了作业配置
@Bean(name = "dailyDataPrepJob")
public Job dailyDataPreparationJob() throws Exception{
return jobBuilderFactory.get("dailyDataPrepJob")
.start(dailyDataPrepStep)
.incrementer(new RunIdIncrementer())
.listener(jobExecutionListener)
.build();
}
有人能告诉我在通过SpringJUnit4ClassRunner执行作业时发生了什么吗?Hi Mayuran,您确定为测试上下文注入了正确的配置吗?(即在CASConfig.class中)。另外,由于您还没有共享launchJob的实现,我希望其中包含使用作业参数触发作业的代码,如JobExecution execution=jobLauncher.run(job,new JobParameters(jobParams))@TharsanSivakumar,很高兴在这里见到您。:D.我没有测试上下文的特定配置。正在使用相同的上下文。我也设法找到了问题所在。它与spring设置无关,JUnit在执行多线程应用程序时出现问题。由于我使用单独的TaskExecutor,所有作业都在单独的线程池中执行。如果您想了解更多信息,请参考以下链接。发生了什么事?日志怎么说?是否正在执行任何步骤?嗨,Mayuran,您确定为测试上下文注入了正确的配置吗?(即在CASConfig.class中)。另外,由于您还没有共享launchJob的实现,我希望其中包含使用作业参数触发作业的代码,如JobExecution execution=jobLauncher.run(job,new JobParameters(jobParams))@TharsanSivakumar,很高兴在这里见到您。:D.我没有测试上下文的特定配置。正在使用相同的上下文。我也设法找到了问题所在。它与spring设置无关,JUnit在执行多线程应用程序时出现问题。由于我使用单独的TaskExecutor,所有作业都在单独的线程池中执行。如果您想了解更多信息,请参考以下链接。发生了什么事?日志怎么说?是否正在执行任何步骤?