How can use an in-memory H2 database when testing my Quarkus application?

左心房为你撑大大i 提交于 2019-12-23 09:58:46

问题


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?


回答1:


Quarkus provides the H2DatabaseTestResource which starts an in memory H2 database as part of the test process.

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

in src/test/resources/application.properties

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

should be set in src/main/resources/application.properties



来源:https://stackoverflow.com/questions/55063778/how-can-use-an-in-memory-h2-database-when-testing-my-quarkus-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!