Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

quarkus

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?

like image 431
geoand Avatar asked Mar 08 '19 13:03

geoand


People also ask

How do I access H2 in-memory database?

Accessing the H2 Console H2 database has an embedded GUI console for browsing the contents of a database and running SQL queries. By default, the H2 console is not enabled in Spring. Then, after starting the application, we can navigate to http://localhost:8080/h2-console, which will present us with a login page.


1 Answers

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.

like image 150
geoand Avatar answered Sep 21 '22 12:09

geoand