在Spring应用程序中运行junit test时访问h2 Web控制台 [英] Access to h2 web console while running junit test in a Spring application

查看:255
本文介绍了在Spring应用程序中运行junit test时访问h2 Web控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Spring应用程序,并且需要在通过Web浏览器运行JUnit测试时检查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.

在Spring配置中,我有一个Bean,它负责创建数据库模式并用一些将在我的JUnit测试中使用的数据填充它.我还在测试上下文中添加了一个bean,该bean创建了一个Web服务器,最终我将在其中查找数据.

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>

一切似乎都可以,因为数据库已正确填充,因为我可以从JUnit测试访问其数据,并且H2 Server仅在测试阶段才运行(我知道这一点,因为如果我尝试访问my_ip:111111,在调试测试之前,我无法连接,但是一旦开始测试,我就可以连接了.

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).

无论如何,如果我从Web浏览器打开H2控制台,则不会显示任何架构.有什么想法吗?

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

非常感谢!

推荐答案

由于这可能是测试调试功能,因此您可以在运行时使用 @Before 添加它:

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();
}

然后连接到 http://localhost:8082/

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

这篇关于在Spring应用程序中运行junit test时访问h2 Web控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆