2

when i want to use @springBatchTest annotation without having a datasource in the project i get this error :

Error creating bean with name 'jobRepositoryTestUtils': Unsatisfied dependency expressed through method 'setDatasource' 

Is there a way to tell @SpringBatchTest Annotation to only create JobLauncherTestUtils and not the JobRepositoryTestUtils or another solution to this problem. Thanks in advance.

1 Answer 1

3

When using @SpringBatchTest, it is expected that the test context contains a bean of type DataSource. This is mentioned in the Javadoc of the annotation, here is an excerpt:

It should be noted that JobLauncherTestUtils requires a Job bean
and that JobRepositoryTestUtils requires a DataSource bean. Since
this annotation registers a JobLauncherTestUtils and a JobRepositoryTestUtils 
in the test context, it is expected that the test context contains
a single autowire candidate for a Job and a DataSource

If all you need is a JobLauncherTestUtils but not other test utilities, you can define a JobLauncherTestUtils bean manually in your test class, something like:

@Bean
public JobLauncherTestUtils testUtils() {
    JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
    jobLauncherTestUtils.setJob(jobUnderTest);
    return jobLauncherTestUtils;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Could you pls guide here: stackoverflow.com/questions/77164606/…?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.