Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

junit

spring

h2

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!!

like image 865
Ivan Fernandez Avatar asked Sep 12 '12 14:09

Ivan Fernandez


People also ask

How do I access H2 console spring?

Accessing the H2 Console By default, the H2 console is not enabled in Spring. Then, after starting the application, we can navigate to http://localhost:8080/h2-console, which will present us with a login page.

How do I access the H2 database console in my browser?

Accessing H2 Console. Start the spring boot application and access the console in the browser with this URL : http://localhost:8080/h2 . We can see the console like this. Now enter the configured username and password.

How do I access my H2 memory database?

To access an in-memory database from another process or from another computer, you need to start a TCP server in the same process as the in-memory database was created. The other processes then need to access the database over TCP/IP or TLS, using a database URL such as: jdbc:h2:tcp://localhost/mem:db1 .


1 Answers

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/

Note: unless you need this to run as part of your CI build, you'll need to remove this code when you're finished debugging

like image 149
snovelli Avatar answered Nov 24 '22 05:11

snovelli