Access to h2 web console while running junit test in a Spring application

Ivan Fernandez picture Ivan Fernandez · Sep 12, 2012 · Viewed 13.3k times · Source

I'm building a Spring application and I need to inspect my H2 in-memory database while I'm running my JUnit tests from a web browser.

In my Spring configuration I have a bean which is responsible of creating my database schema and populating it with some data which will be used within my JUnit tests. I've also added a bean in my test context which creates a web server where I eventually will look for my data.

<bean id="org.h2.tools.Server-WebServer" class="org.h2.tools.Server"
    factory-method="createWebServer" init-method="start" lazy-init="false">
    <constructor-arg value="-web,-webAllowOthers,-webPort,11111" />
</bean>

Everything seems ok because the database is populated properly since I can access to its data from my JUnit tests and H2 Server only runs while I'm in my test-phase (I can know that, because if I try to access to my_ip:111111 before debugging my tests I cannot connnect but I can connect afterwards once I've started my tests).

Anyway If I open my H2 console from a web browser no schema is shown in it. Any ideas??

Many thanks!!

Answer

snovelli picture snovelli · Dec 3, 2015

As this is probably going to be a test-debugging feature, you can add it at runtime with your @Before:

import org.h2.tools.Server;

/* Initialization logic here */

@BeforeAll
public void initTest() throws SQLException {
    Server.createWebServer("-web", "-webAllowOthers", "-webPort", "8082")
    .start();
}

And then connect to http://localhost:8082/