I followed this link and prepared a JUnit test case cannot load application context from junit for spring batch job. However I get following exception upon running a test.
Error creating bean with name 'fileLoadTest.JobconfigurationTest': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.test.FileLoadTest' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:
Please let me know if anyone had similar issue and how to solve. Thanks in advance.
@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();
}
}
}
Working code for the different job.
@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());
}