Access to h2 web console while running junit test

2020-02-08 08:29发布

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

标签: spring junit h2
3条回答
我欲成王,谁敢阻挡
2楼-- · 2020-02-08 08:50

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 */

@Before
public void initTest(){
    Server webServer = Server.createWebServer("-web", 
                                    "-webAllowOthers", "-webPort", "8082");
    webServer.start();
}

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

查看更多
家丑人穷心不美
3楼-- · 2020-02-08 09:02

For future reference here's another way to do it:

  1. Start database and web servers (version can differ):

    $ cd .../maven_repository/com/h2database/h2/1.4.194 $ java -cp h2-1.4.194.jar org.h2.tools.Server -tcp -web -browser TCP server running at tcp://169.254.104.55:9092 (only local connections) Web Console server running at http://169.254.104.55:8082 (only local connections)

  2. Set database url for tests in code to jdbc:h2:tcp://localhost:9092/mem:mytest.

  3. Run or debug tests.
  4. Click Connect in browser window which opened in step 1.

Jar file for H2 can be downloaded at https://mvnrepository.com/artifact/com.h2database/h2.

Server can be started via @Before in test file like in snovelli's answer, but only in case connection to database in established afterwards, which might be a problem.

查看更多
劳资没心,怎么记你
4楼-- · 2020-02-08 09:02

I guess the problem is that you are connecting to h2db directly from your application. Not through the server you are launching with bean. Because of this your app and h2db-web-interface can't share one in-memory database.

You should change jdbcUrl in tests to something like jdbc:h2:tcp://localhost/mem:my_DB;DB_CLOSE_DELAY=-1;MODE=Oracle and in browser you should connect to the same url.

With jdbc urls like jdbc:h2:tcp://localhost/... all connections will go through the h2db-server and you can view database state in browser.

查看更多
登录 后发表回答