15

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?

geoand
  • 60,071
  • 24
  • 172
  • 190

2 Answers2

22

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

Update

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.

geoand
  • 60,071
  • 24
  • 172
  • 190
  • I tried to use this approach in my sample, but failed. It reported the connection timeout. https://stackoverflow.com/questions/57900569/exception-occured-in-test-when-add-hibernate-orm-panache-in-quarkus – Hantsy Sep 12 '19 at 06:50
1

You can use the below configurations in the application.properties file to use h2 database

    quarkus.datasource.jdbc.url=jdbc:h2:mem:default
    quarkus.datasource.driver=org.h2.Driver
    quarkus.datasource.username=admin
    quarkus.hibernate-orm.database.generation=drop-and-create