我遵循了此链接,并准备了一个JUnit测试用例cannot load application context from junit for spring batch job。但是,运行测试时出现以下异常。
创建名称为'fileLoadTest.JobconfigurationTest'的bean时出错:通过构造函数参数0表示的不满意依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'com.test.FileLoadTest'的合格Bean:应该至少有1个有资格作为自动装配候选的Bean。依赖注释:
请让我知道是否有人遇到类似问题以及如何解决。预先感谢。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBatchTest
@ContextConfiguration(classes = { DataSourceConfiguration.class, SecondaryDataSourceConfiguration.class, FileLoadTest.JobconfigurationTest.class }, initializers=ConfigFileApplicationContextInitializer.class)
@TestPropertySource("classpath:application-test.properties")
@ActiveProfiles("test")
public class FileLoadTest{
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
@Qualifier("fileLoadJob")
Job fileLoadJob;
@Test
public void testFileLoadJob() throws Exception {
JobParameters jobParameters = new JobParametersBuilder()
.addString("inputFile", "testdata.csv")
.addLong("time", System.currentTimeMillis()).toJobParameters();
JobExecution jobExecution = this.jobLauncherTestUtils.launchStep("fileLoadJob", jobParameters);
Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
}
@Configuration
public class JobconfigurationTest {
@Bean
public JobLauncherTestUtils jobLauncherTestUtils() {
return new JobLauncherTestUtils();
}
}
}
另一份工作的工作代码。
@RunWith(SpringRunner.class)
@SpringBatchTest
@ContextConfiguration(classes = { BatchConfigurationCleanupStep.class, DataSourceConfiguration.class, SecondaryDataSourceConfiguration.class })
@TestPropertySource("classpath:application.properties")
@ActiveProfiles("test")
public class CleanupJobTest{
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@MockBean
CleanupTasklet cleanupTasklet;
@MockBean
StepExecutionListener stepExecutionListener;
@Test
public void testCleanupJob() throws Exception {
long batchId = 1234;
String purgeType = "DELETE";
// given
JobParameters jobParameters = new JobParametersBuilder()
.addLong("batchId", batchId)
.addString("purgeType", purgeType)
.addLong("time", System.currentTimeMillis()).toJobParameters();
// when
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);
// then
Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
}