Using H2 console during test debugging in Spring

Hubert Schumacher
2 min readDec 19, 2020

How do I use H2 to verify the database content during debugging sessions of integration test?

Configure your Spring project to enable H2 console

The application.yml file can be configured as follows (example). If you use application.properties, the same settings would apply but in the typical properties format.

spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
name:
username: sa
password:
hikari:
auto-commit: false
h2:
console:
enabled: true
settings:
web-allow-others: true
trace: true

Set your Breakpoints

In you IDE, when setting breakpoints, verify that the breakpoints you set, suspend the Thread only and not all threads. All Threads would also suspend the H2 server thread.

Breakpoint should only suspend current thread

In Intellij this is done my right-clicking on the Breakpoint and select Suspend: Thread.

Then you can run your test in debugging configuration and inspect the database content with the H2 console.

Inspect data with H2-Console

If note configured differently, H2 console will be available through the url

http://localhost:8080/h2-console

Now you can login to the H2 server and access the database content while selecting the data-source url and the username and password from your configuration in application.yml.

H2-Console login
Analyzing data with the H2-Console (example)

Happy debugging!!

--

--

Hubert Schumacher
0 Followers

Web entrepreneur and ICT expert with focus on Semantic information technologies, as well as Software Quality and Testing