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

标签 java spring-boot spring-batch

我正在为 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/

相关文章:

Spring Boot Actuator 将跟踪端点信息写入文件

java - MyNotificationService 需要类型为 'org.springframework.mail.javamail.JavaMailSender' 的 bean,但无法找到

java - 在 Spring Batch 中的 beforeStep 中停止作业

java - 尝试动态实例化类

java - 如何在java中的webservices中返回json作为响应

java - Android 中使用 Java 向 SQLITE 数据库插入数据失败?

Spring boot 2.1.3 从 2.0.6 更新导致 BeanDefinitionParsingException 用于 util :map

mongodb - Spring Boot - 将计划的作业作为单独的进程运行

java - Spring Batch 在运行步骤之前解析步骤的资源

java - Jetty:将 .war 解压到特定文件夹