1

I am trying to write Integration test for spring batch aplication , in my project there are approx 10+jobs I want to run only a single job but unable to achieve any Suggestion.

@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes= MyApp.class)
@SpringBootTest
@Slf4j
public class JobATest {
    

    JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();

    @Autowired
    @Qualifier(JOB_A)
    Job joba;

    @Before
    public void setUp() throws Exception {
        log.debug("CAME HERE  setUp {} ",joba.getName());
        jobLauncherTestUtils.setJob(joba);
    }

    @After
    public void tearDown() throws Exception {
    }
   
    @Test
    public void processAJob() throws Exception {
        jobLauncherTestUtils.launchJob();
    } 
}

ERROR

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'jobLauncherTestUtils': Unsatisfied dependency expressed 
through method 'setJob' parameter 0; nested exception is 
org.springframework.beans.factory.NoUniqueBeanDefinitionException:
 No qualifying bean of type 'org.springframework.batch.core.Job' available: 
expected single matching bean but found 2: **joba,jobb**

Vipul Pandey
  • 1,507
  • 11
  • 20

1 Answers1

0

When using @SpringBatchTest, it is expected that the test context contains a single job bean. This is mentioned in the javadoc of the annotation.

There is an open issue for that which we might consider for the next major release. Please upvote or add a comment if you have a suggestion for an improvement. I also invite you to check the thread on Multiple Job unit testing with @SpringBatchTest which could help you as well.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • Can we use any other approach for testing. – Vipul Pandey May 04 '22 at 13:57
  • Removing the usage of `@SpringBatchTest` should fix your issue since you are already defining the `JobLauncherTestUtils` and setting the job under test manually in `setUp`. – Mahmoud Ben Hassine May 04 '22 at 15:37
  • I tried removing it and setting via manually but its throwing No qualifying bean of type 'org.springframework.batch.test.JobLauncherTestUtils – Vipul Pandey May 05 '22 at 05:55
  • Are you sure your change has been taken into account? I don't see how this is possible. From what you shared, nothing is requesting a bean of type `JobLauncherTestUtils`, so I can't see how you can get this error. – Mahmoud Ben Hassine May 05 '22 at 12:56