【发布时间】:2019-05-26 14:46:12
【问题描述】:
我正在尝试使用 Mybatis 使用 H2 内存数据库运行 Springboot 测试。 到目前为止,我已经完成了
- 在 application-test.properties 中配置 h2 DB
- 添加注释
@SpringBootTest, @TestPropertySource (locations = "TEST_APPLICATION_PROPERTIES_LOCATION")
- 自动装配 dao 和 serviceImpl bean
- 使用 将 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") })
尽管采取了上述措施,我仍然有两个问题
我无法检索使用 seed.sql 输入的用户。我创建了一个 id="admin"、pw="admin" 的用户,并尝试使用 findById("admin") 进行检索。但它总是返回 null。
在使用 @test 进行调试时,我无法打开 h2 DB。我根本无法使用 localhost:8080/h2-console 访问 h2(路径已明确写入 application-test.properties)
我应该采取什么额外措施来使用 h2 测试 SpringBoot?
【问题讨论】:
标签: database unit-testing spring-boot h2 mybatis