Spring Batch JUnit5测试报错:找不到javax.sql.DataSource Bean
解决Spring Batch单元测试中找不到DataSource的问题
核心原因是@SpringBatchTest引入的JobRepositoryTestUtils依赖javax.sql.DataSource Bean,但你的测试上下文仅加载了ListenerJobConfiguration,未配置DataSource也未触发Spring Boot自动配置逻辑,导致容器中不存在符合要求的DataSource Bean。
以下是三种可行解决方案:
方案1:添加内存数据库依赖 + 启用Spring Boot自动配置
通过引入H2内存数据库,结合Spring Boot测试注解自动配置DataSource:
1. 添加依赖
Maven(pom.xml):
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency>
Gradle(build.gradle):
testImplementation 'com.h2database:h2'
2. 修改测试类
用@SpringBootTest替代@ContextConfiguration,触发Spring Boot自动配置:
@SpringBatchTest @SpringBootTest(classes = ListenerJobConfiguration.class) class ApplicationTests { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @Autowired private JobRepositoryTestUtils jobRepositoryTestUtils; @BeforeEach public void clearJobExecutions() { this.jobRepositoryTestUtils.removeJobExecutions(); } @Test public void testMyJob() throws Exception { JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters(); JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters); Assertions.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); } }
方案2:测试类中手动定义DataSource Bean
在测试上下文里直接配置内存DataSource,无需额外依赖(需确保H2驱动已引入):
@SpringBatchTest @ContextConfiguration(classes = {ListenerJobConfiguration.class, ApplicationTests.TestDataSourceConfig.class}) class ApplicationTests { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @Autowired private JobRepositoryTestUtils jobRepositoryTestUtils; @BeforeEach public void clearJobExecutions() { this.jobRepositoryTestUtils.removeJobExecutions(); } @Test public void testMyJob() throws Exception { JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters(); JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters); Assertions.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); } @Configuration static class TestDataSourceConfig { @Bean public DataSource dataSource() { SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); dataSource.setDriverClass(org.h2.Driver.class); dataSource.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1"); dataSource.setUsername("sa"); dataSource.setPassword(""); return dataSource; } } }
方案3:使用嵌入式内存JobRepository
若无需持久化Job执行记录,可配置MapJobRepository(完全内存级,无需DataSource):
@SpringBatchTest @ContextConfiguration(classes = {ListenerJobConfiguration.class, ApplicationTests.TestBatchConfig.class}) class ApplicationTests { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @Test public void testMyJob() throws Exception { JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters(); JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters); Assertions.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); } @Configuration static class TestBatchConfig { @Bean public JobRepository jobRepository() throws Exception { return new MapJobRepositoryFactoryBean().getObject(); } @Bean public JobLauncher jobLauncher(JobRepository jobRepository) { SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); jobLauncher.setJobRepository(jobRepository); jobLauncher.afterPropertiesSet(); return jobLauncher; } } }
内容的提问来源于stack exchange,提问作者Jeff Cook
相关产品推荐
相关产品推荐

