【问题标题】:Spring Boot Integration test with H2 inmemory database在内存数据库中使用 H2 进行 Spring Boot 集成测试
【发布时间】:2017-03-22 05:28:18
【问题描述】:

我尝试了各种方法,但现在我很困惑,我有 spring boot 应用程序,我正在为它编写与 inmemory h2 数据库的集成测试。

这是 application.yml

spring.profiles: test
spring.datasource:
    url: jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    username: sa
    password:
    driver-class-name: org.h2.Driver
    platform: h2
    # enable H2 web console and set url for web console
      # http://localhost:8080/console
    h2:
      console:
        enabled: true
        path: /console
    schema: schema.sql
    data: data.sql

我通过从这里启动服务器来启动控制台以在测试的调试模式下对其进行测试

@Bean
public ServletRegistrationBean h2servletRegistration() throws SQLException {
    Server webServer = Server.createWebServer("-web", "-webAllowOthers", "-webPort", "8081");
    webServer.start();
    ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
    registration.addUrlMappings("/console/*");
    return registration;
}

我可以在 8081 端口上查看控制台,但我看不到我从 yml 文件中填充的架构。请帮忙。

【问题讨论】:

  • 尝试使用模式,例如: MODE=MYSQL in your url
  • 你能看到正在加载到数据库中的 sql 吗?
  • @GurinderSPanesar 没有运气
  • @AdamSmith 我正在尝试通过控制台查看此内容,我无法从那里看到,如何检查?
  • 我有一个疑问,我这样做对吗?我正在启动服务器,并且在 yml 中也有配置?如果这会创建两个不同的内存数据库?

标签: java spring spring-boot integration-testing h2


【解决方案1】:

我不知道为什么这不起作用,但我通过 java 代码为数据源创建一个 bean 并从那里加载脚本来让他工作。

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 2019-05-17
    • 2020-03-09
    • 2019-02-02
    • 1970-01-01
    • 2020-03-20
    • 2016-12-24
    • 2019-04-01
    • 2019-03-03
    相关资源
    最近更新 更多