Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to HSQLDB in-memory in Spring Boot tests to make a query

I have a Spring Boot application with mysql db, but when running tests HSQLDB is used. Is it possible to connect to this in-memory database to execute a query? What I have to change in tests/application to run it? I've already tried to run hsql database manager and connect to this database but it seems to be empty but actually it is not empty because in the application I'm debugging the code and read data from this database.

like image 510
Maad Maad Avatar asked Sep 12 '25 07:09

Maad Maad


1 Answers

You have two options.

One option is to start an HyperSQL (HSQLDB) Server in Spring. This option is discussed here in the answer How to start HSQLDB in server mode from Spring boot application You then start the HyperSQL DatabaseManagerSwing in a separate Java process and connect to the HyperSQL Server with the URL: jdbc:hsqldb:hsql://localhost/testdb.

The alternative option is to start the HyperSQL DatabaseManagreSwing in Spring. You can modify the bean template given for the Server and start org.hsqldb.util.DatabaseManagerSwing instead of the Server. In this case, the DatabaseManager should connect with the URL: jdbc:hsqldb:mem:testdb.

Note in both cases, you need to specify the jdbc:hsqldb:mem:testdb URL as the Spring data source.

like image 69
fredt Avatar answered Sep 14 '25 21:09

fredt