【发布时间】:2012-03-08 21:55:51
【问题描述】:
我正在尝试对我的 DAO 进行单元测试(使用 Spring 和 Hibernate)。我按照this 教程使用 HSQLDB。该教程指出内存中的 HSQLDB 数据库可以使用 SQL 脚本进行初始化,但我找不到有关如何在 Spring 中执行此操作的信息。这是相关的 Spring 上下文配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:mydb" />
<property name="username" value="sa" />
<property name="password" value="" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="10" />
</bean>
任何帮助将不胜感激。谢谢。
【问题讨论】:
-
The difference between the in-memory and the file mode is that the in-memory database is empty, but the file mode is initialized with data. One strategy that I have employed in the past is to create a standalone database, allow Hibernate to create the tables and add data for me, save the data into a script, and then use the file-based URL to point to the script. The good thing about the script is that it is raw SQL so you are free to pre-populate the database with whatever data you want to test against.这个来自你链接的帖子,它清楚地提到了这个过程。 -
我阅读了上面的内容,但我想我没有把 2 和 2 放在一起,然后你会使用 HSQLDB 的“文件”版本,它会在内存中使用脚本作为启动.