测试 Quarkus 应用程序时如何使用内存中的 H2 数据库? [英] How can use an in-memory H2 database when testing my Quarkus application?
问题描述
我计划使用 PostgreSQL 作为我的 Quarkus 应用程序的数据库,但我希望在我的测试中使用 H2 的便利性.
I plan to use PostgreSQL as the database for my Quarkus application but I would like the convenience of using H2 in my tests.
有什么办法可以完成这样的壮举吗?
Is there a way I can accomplish such a feat?
推荐答案
Quarkus 提供了 H2DatabaseTestResource 作为测试过程的一部分启动内存 H2 数据库.
Quarkus provides the H2DatabaseTestResource which starts an in memory H2 database as part of the test process.
您需要将 io.quarkus:quarkus-test-h2
添加为 test
范围的依赖项,并使用 @QuarkusTestResource(H2DatabaseTestResource.class) 注释您的测试)
.您还需要具备以下条件:
You will need to add io.quarkus:quarkus-test-h2
as a test
scoped dependency and annotate your test with @QuarkusTestResource(H2DatabaseTestResource.class)
.
You will also need to have something like:
quarkus.datasource.url=jdbc:h2:tcp://localhost/mem:test
quarkus.datasource.driver=org.h2.Driver
在src/test/resources/application.properties
为了让应用程序使用 PostgreSQL 作为其常规运行的一部分,quarkus-jdbc-postgresql
应该是一个依赖项,并且
In order for the application use PostgreSQL as part of its regular run, quarkus-jdbc-postgresql
should be a dependency and
quarkus.datasource.url=jdbc:postgresql://mypostgres:5432
quarkus.datasource.driver=org.postgresql.Driver
应该在src/main/resources/application.properties
更新
从 1.13
版本开始,当 quarkus-jdbc-h2
在类路径上并且没有提供 URL 配置时,Quarkus 可以在开发和测试模式下自动启动 H2.请参阅此了解更多信息.
As of version 1.13
, Quarkus can launch H2 automatically in dev and test mode when quarkus-jdbc-h2
is on the classpath and no URL configuration is provided.
See this for more information.
这篇关于测试 Quarkus 应用程序时如何使用内存中的 H2 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!