非Web Spring引导应用程序的集成测试

时间:2019-08-08 20:54:36

标签: spring-boot spring-boot-test

说明
我有一个非Web应用程序的Spring Boot应用程序。这是一项计划作业,因此会定期运行以检查Amazon SQS队列。如果有消息,我将继续执行消息中的参数所提供的工作

实际问题
我正在尝试使用Maven运行集成测试。 我在pom.xml中有一个集成配置文件,它将仅运行集成测试

mvn verify -Pintegration

application-test.yml

spring.datasource:
  url: ${DBCONNECTIONSTRING}
  type: com.zaxxer.hikari.HikariDataSource

其中DBCONNECTIONSTRING是我的sql连接字符串,具有用户名和密码。它是一个环境变量。另外,DBCONNECTIONSTRING指向活动数据库。我需要验证数据是否已更新。

IntegrationTest.java

@RunWith(SpringRunner.class)
@SpringBootTest(classes = *****(SpringApplication).class)
@ActiveProfiles("test")
public class IntegrationTest {

  @Autowired
  DependencyCheckService dependencyCheckService;

  @Test()
  public void integration() {
    Assert.assertTrue(dependencyCheckService.dbcheck());
  }
}

DependencyCheckImpl.java

@Service
public class DependencyCheckServiceImpl implements DependencyCheckService {

  @Autowired
  JdbcTemplate template;

  public boolean dbCheck() {
    try {
      List<Object> results = template.query("select 1 from dual", new SingleColumnRowMapper<>());
      if (results.size() == 1) {
        return true;
      }
    } catch (Exception e) {
      if (LOGGER.isErrorEnabled()) {
        LOGGER.error("Error when accessing database. " + e.getMessage());
      }
    }
    return false;
  }

当我在Docker上运行它时。我收到以下错误

[ERROR] integration(com.bt.package.name.integrationtests.IntegrationTest)  Time elapsed: 0.001 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: URL must start with 'jdbc'
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: URL must start with 'jdbc'
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: URL must start with 'jdbc'
Caused by: java.lang.IllegalArgumentException: URL must start with 'jdbc'

我对配置有何疑问?
[编辑] DBCONNECTIONSTRING实际上以jdbc开头。它在我的环境变量中。可能有原因为什么测试yml无法从环境中正确读取它?

0 个答案:

没有答案