我正在构建一个Spring应用程序,当我从Web浏览器运行JUnit测试时,我需要检查我的H2内存数据库.
在我的Spring配置中,我有一个bean,它负责创建我的数据库模式并用一些将在我的JUnit测试中使用的数据填充它.我还在我的测试环境中添加了一个bean,它创建了一个Web服务器,我最终将查找我的数据.
<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>
Run Code Online (Sandbox Code Playgroud)
一切似乎都没问题,因为数据库已正确填充,因为我可以从我的JUnit测试中访问其数据,H2服务器仅在我处于测试阶段时运行(我可以知道,因为如果我尝试访问my_ip:111111在调试我的测试之前,我无法连接,但是一旦我开始测试,我就可以连接了.
无论如何如果我从Web浏览器打开我的H2控制台,它中没有显示架构.有任何想法吗??
非常感谢!!
sno*_*lli 20
由于这可能是一个测试调试功能,您可以在运行时使用@Before添加它:
import org.h2.tools.Server;
/* Initialization logic here */
@Before
public void initTest(){
Server webServer = Server.createWebServer("-web",
"-webAllowOthers", "-webPort", "8082");
webServer.start();
}
Run Code Online (Sandbox Code Playgroud)
为了将来参考,这里有另一种方法:
启动数据库和 Web 服务器(版本可能不同):
$ 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)
将代码中测试的数据库 url 设置为jdbc:h2:tcp://localhost:9092/mem:mytest.
Connect在步骤 1 中打开的浏览器窗口中单击。H2 的 Jar 文件可以在https://mvnrepository.com/artifact/com.h2database/h2下载。
服务器可以通过@Before在测试文件中启动,就像在 snovelli 的答案中一样,但只有在之后建立与数据库的连接的情况下,这可能是一个问题。
小智 2
我猜问题是您直接从应用程序连接到 h2db。不是通过您使用 bean 启动的服务器。因此,您的应用程序和 h2db-web-interface 无法共享一个内存数据库。
您应该将测试中的 jdbcUrl 更改为类似的内容jdbc:h2:tcp://localhost/mem:my_DB;DB_CLOSE_DELAY=-1;MODE=Oracle,并且在浏览器中您应该连接到相同的 url。
使用 jdbc url,jdbc:h2:tcp://localhost/...所有连接都将通过 h2db-server,您可以在浏览器中查看数据库状态。
| 归档时间: |
|
| 查看次数: |
9396 次 |
| 最近记录: |