当我想在项目中没有数据源的情况下使用 @springBatchTest 注释时,我收到此错误:
Error creating bean with name 'jobRepositoryTestUtils': Unsatisfied dependency expressed through method 'setDatasource'
有没有办法告诉 @SpringBatchTest Annotation 仅创建 JobLauncherTestUtils 而不是 JobRepositoryTestUtils 或此问题的其他解决方案。 提前致谢。
最佳答案
使用@SpringBatchTest
时,预计测试上下文包含DataSource
类型的bean。 Javadoc中提到了这一点注释的摘录如下:
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
如果您只需要一个 JobLauncherTestUtils
而不是其他测试实用程序,则可以在测试类中手动定义一个 JobLauncherTestUtils
bean,如下所示:
@Bean
public JobLauncherTestUtils testUtils() {
JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
jobLauncherTestUtils.setJob(jobUnderTest);
return jobLauncherTestUtils;
}
关于spring-batch - @springBatchTest 没有数据源错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69190751/