【问题标题】:What is the proper way to test SpringBoot Database with H2?用 H2 测试 Spring Boot 数据库的正确方法是什么?
【发布时间】:2019-05-26 14:46:12
【问题描述】:

我正在尝试使用 Mybatis 使用 H2 内存数据库运行 Springboot 测试。 到目前为止,我已经完成了

  1. 在 application-test.properties 中配置 h2 DB
  2. 添加注释
@SpringBootTest, @TestPropertySource (locations = "TEST_APPLICATION_PROPERTIES_LOCATION")
  1. 自动装配 dao 和 serviceImpl bean
  2. 使用
  3. 将 seed.sql 和 purge.sql 添加到测试类
@SqlGroup({
        @Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:/database/seed.sql"),
        @Sql(executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:/database/purge.sql") })

尽管采取了上述措施,我仍然有两个问题

  1. 我无法检索使用 seed.sql 输入的用户。我创建了一个 id="admin"、pw="admin" 的用户,并尝试使用 findById("admin") 进行检索。但它总是返回 null。

  2. 在使用 @test 进行调试时,我无法打开 h2 DB。我根本无法使用 localhost:8080/h2-console 访问 h2(路径已明确写入 application-test.properties)

我应该采取什么额外措施来使用 h2 测试 SpringBoot?

【问题讨论】:

    标签: database unit-testing spring-boot h2 mybatis


    【解决方案1】:

    在您的属性文件中添加spring.h2.console.enabled=true

    【讨论】:

    • 是的,我已将其添加到 application.properties,但仍然无法进入控制台。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2019-05-17
    • 1970-01-01
    • 2022-01-04
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多