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.

6 comments:

Unknown said...
This comment has been removed by a blog administrator.
Unknown said...
This comment has been removed by a blog administrator.
aliyaa 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:

xcom 2 god mode said...

Hi, I tried this and this worked fine in netbeans.. I try to run in console via java -jar and jar.name . but i always got

molasses benefits for hair 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: