About the author

My photo

Java, Scala, and everything

Friday, March 20, 2015

Browse H2 in-memory database in tests

If you sometimes want to look into H2 tables during the test, there is a problem, because by default in-memory H2 database is accessible only from the process where it was created. Simple way to browse the data looks like this:

Server.createWebServer().start()

It starts the web server on port 8082, so you can connect there, enter DB url, username and password (usually username and password are empty, you don’t need to protect your in-memory test database, do you?) and voila! You can browse the data.

Just remember to put some breakpoint in the test code, because after the test the process is finished, and so is the server. Another thing to remember is to set the breakpoint to stop only current thread, not all threads (like it is by default in Idea), because if you stop all of them, the server one is also stopped :)

Written with StackEdit.

4 comments:

Dev Soft said...
This comment has been removed by a blog administrator.
Rosen Rose said...
This comment has been removed by a blog administrator.
aliya seen said...
This comment has been removed by a blog administrator.
ark item id said...

I followed the example above but JdbcTemplate keeps connecting to my Postgres database instead of the H2 database. I guess it picks up the Postgres database connection parameters from the application.properties file. Any idea how to resolve this? Here are my settings on the pom file: