0

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());
    }
Prashant S
  • 349
  • 4
  • 14
  • 1
    `@SpringBatchTest` already adds a bean of type `JobLauncherTestUtils` to the test context (see its [javadoc](https://docs.spring.io/spring-batch/docs/4.3.x/api/org/springframework/batch/test/context/SpringBatchTest.html)), so you can remove the `JobconfigurationTest` class. Does this fix your issue? – Mahmoud Ben Hassine Nov 04 '20 at 09:23
  • Thank you Mahmoud. I've removed `JobconfigurationTest` and it gave me jobRepository error. I autowired it but failed with this exception - `Error creating bean with name 'jobLauncherTestUtils': Unsatisfied dependency expressed through method 'setJobRepository' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.batch.core.repository.JobRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}` – Prashant S Nov 04 '20 at 15:53
  • 1
    You are autowiring a `Job` and a `JobRepository` beans in your test class, where are those defined? – Mahmoud Ben Hassine Nov 04 '20 at 17:04
  • Not sure if that is causing any problem? I referred shared document link and updated code but still failing for the basic test scenario. I've been trying since a couple of days. I've updated code. – Prashant S Nov 04 '20 at 21:18
  • According to your update: `No qualifying bean of type 'org.batch.core.listener.JobCompletionNotificationListener`, this means you are trying to inject this listener but no bean of that type is defined. Please provide a [minimal complete example](https://stackoverflow.com/help/minimal-reproducible-example) that reproduces the issue to be able to help you in an efficient way. – Mahmoud Ben Hassine Nov 05 '20 at 08:06
  • 1
    I've updated working code of a different job but thanks for your help Mahmoud. Your example in javadoc really helped to resolve the issue. – Prashant S Nov 06 '20 at 21:36

0 Answers0