Home > Database > Mysql Tutorial > body text

Spring建立虚拟DB环境进行单测

WBOY
Release: 2016-06-07 17:31:47
Original
1187 people have browsed it

在进行DAO本地测试或者单元测试时,db环境有时很难弄,而且即使调通了db环境,有时也不想往db里面灌些无用数据,所以,为了不对真

在进行DAO本地测试或者单元测试时,db环境有时很难弄,而且即使调通了db环境,有时也不想往db里面灌些无用数据,所以,为了不对真实db环境造成侵入,spring提供了一套虚拟db环境,,利用它可以方便地进行单元测试。

在spring环境中配置:




type支持HSQL、H2、Derby三种。默认为HSQL
schema.sql文件为建库建表语句,data.sql为表数据文件,初始可以先insert一些数据。

配置embedded database之后,就可以将其注入到需要dataSource的DAO中,之后就可以进行DAO单元测试了。

另外,还有一种代码方式来创建embedded database:
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.type(H2).script("schema.sql").script("test-data.sql").build();
// do stuff against the db (EmbeddedDatabase extends javax.sql.DataSource)
db.shutdown();

相关阅读:

运用Spring注解实现Netty服务器端UDP应用程序

Spring2.5.6+Hibernate3+RMI整合

Struts2与Spring整合步骤

 

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!