gpt4 book ai didi

java - Spring Batch 测试用例忽略 Spring 配置文件

转载 作者:行者123 更新时间:2023-12-02 02:30:37 25 4
gpt4 key购买 nike

我正在为 Spring Batch 应用程序进行 PoC。到目前为止,一切正常,但我无法让测试来获取数据源,它总是失败:

...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 42 more

这是我的 application.yml 文件:

server:
port: ${port:9080}
spring:
http:
encoding:
charset: UTF-8
enabled: true
force: true
output:
ansi:
enabled: detect
profiles: # default, development, production
active: default, development
---
spring:
datasource:
driver-class-name: org.h2.Driver
sql-script-encoding: UTF-8
url: jdbc:h2:mem:JavaSpringBootBatch;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MySQL
username: sa
password:
h2:
console:
enabled: true
path: /h2-console
profiles: development
---
server:
port: ${port:80}
spring:
datasource:
initialize: false
# ...
profiles: production

...以及我的测试类:

@Configuration
@Import(AppConfig.class) // This eventually "imports" the app's config
public class TestConfig {
@Bean
public JobLauncherTestUtils jobLauncherTestUtils() {
return new JobLauncherTestUtils();
}
}
<小时/>
@ActiveProfiles("development")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
//@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })
public class PlayerItemBatchTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;

@Test
public void readProcessWriteBatchProcess() throws Exception {
final JobExecution jobExecution = jobLauncherTestUtils.launchJob();
Assertions.assertThat(jobLauncherTestUtils.launchJob().getStatus()).isEqualTo(BatchStatus.COMPLETED);
}
}
<小时/>
@Configuration
public class BatchConfig { // Included with AppConfig
private final DataSource dataSource;

@Autowired
public BatchConfig(final DataSource dataSource) {
this.dataSource = dataSource;
}

// ...
}

当我启动应用程序时,我可以清楚地看到 datasource 拥有我在 YAML 的 development 配置文件中拥有的所有内容(所有设置)。当我尝试执行测试用例时,它只是失败并提示“找不到数据源”。

最佳答案

除了使用@EnableConfigurationProperties尝试在 test/resources 中创建一个 .yml 文件,并使用 @TestConfiguration 注解在 test/src 中创建一个 TestConfig.java 。

关于java - Spring Batch 测试用例忽略 Spring 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47182775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com