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.