用于测试的嵌入式 H2 数据库的 Spring 配置 [英] Spring configuration for embedded H2 database for tests
问题描述
使用 嵌入式 h2 数据源 进行集成测试的 Spring 配置是什么样的?,可选,JUnit?
我第一次尝试 SingleConnectionDataSource 基本上可以工作,但在更复杂的测试中失败,您需要同时进行多个连接或暂停事务.我认为 基于 tcp 的服务器模式中的 h2 可能有效也可以,但是对于内存中的临时嵌入式数据库,这可能不是最快的通信方式.
有哪些可能性及其优点/缺点?另外,您如何创建表/填充数据库?
<小时>更新:让我们指定一些对此类测试很重要的具体要求.
- 数据库应该是临时的并且在内存中
- 出于速度要求,连接可能不应该使用 tcp
- 如果我能在调试过程中使用数据库工具检查数据库的内容就好了
- 我们必须定义一个数据源,因为我们不能在单元测试中使用应用服务器数据源
保留不知道有没有什么工具可以检查数据库,我认为一个简单的解决方案是使用Spring嵌入式数据库(3.1.x 文档,当前文档),支持 HSQL、H2 和 Derby.
使用 H2,您的 xml 配置将如下所示:
<jdbc:script location="classpath:db-schema.sql"/><jdbc:script location="classpath:db-test-data.sql"/></jdbc:embedded-database>
如果你更喜欢基于 Java 的配置,你可以像这样实例化一个 DataSource
(注意 EmbeddedDataBase
扩展了 DataSource
):
@Bean(destroyMethod = "shutdown")公共嵌入式数据库数据源(){返回新的 EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("db-schema.sql").addScript("db-test-data.sql").建造();}
数据库表由 db-schema.sql 脚本创建,并填充了来自 db-test-data.sql 脚本的测试数据.>
不要忘记将 H2 数据库驱动程序添加到您的类路径中.
What does your Spring configuration for integration tests look like using an embedded h2 datasource and, optionally, JUnit?
My first try with a SingleConnectionDataSource basically worked, but failed on more complicated tests where you need several connections at the same time or suspended transactions. I think h2 in tcp based server mode might work as well, but this is probably not the fastest communication mode for a temporary embedded database in memory.
What are the possibilities and their advantages / disadvantages? Also, how do you create the tables / populate the database?
Update: Let's specify some concrete requirements that are important for such tests.
- The database should be temporary and in memory
- The connection should probably not use tcp, for speed requirements
- It would be nice if I could use a database tool to inspect the content of the database during debugging
- We have to define a datasource since we can't use the application servers datasource in unit tests
With the reservation that I do not know if there is any tool that can inspect the database, I think that a simple solution would be to use the Spring embedded database (3.1.x docs, current docs) which supports HSQL, H2, and Derby.
Using H2, your xml configuration would look like the following:
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:db-schema.sql"/>
<jdbc:script location="classpath:db-test-data.sql"/>
</jdbc:embedded-database>
If you prefer Java based configuration, you can instantiate a DataSource
like this (note that EmbeddedDataBase
extends DataSource
):
@Bean(destroyMethod = "shutdown")
public EmbeddedDatabase dataSource() {
return new EmbeddedDatabaseBuilder().
setType(EmbeddedDatabaseType.H2).
addScript("db-schema.sql").
addScript("db-test-data.sql").
build();
}
The database tables are created by the db-schema.sql script and they are populated with test data from the db-test-data.sql script.
Don't forget to add the H2 database driver to your classpath.
这篇关于用于测试的嵌入式 H2 数据库的 Spring 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!