spring-batch - @springBatchTest 没有数据源错误

标签 spring-batch

当我想在项目中没有数据源的情况下使用 @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/

相关文章:

java - 如何在每个月的每个工作日运行 quartz 作业

java - StepBuilder.chunk() 使用mockito返回null

Spring 批处理 : Fault Tolerant

java - 在创建应用程序 bean 之前初始化 Spring Batch DB

java - Spring Batch 中未终止的双引号

Spring Batch - 读取大型平面文件 - 水平缩放的选择?

java - StaxEventItemWriter - 文件不可写问题

java - 如果 template.convertSendAndReceive 消费者消息处理程序抛出异常,则抛出异常?

java - 如何按顺序初始化步骤?

java - Spring 批处理 : How to create a Composite Item Writer?