淘宝商城(天猫)高级技术专家.3年研发+3年性能测试调优/系统测试+4年团队管理与测试架构、研发系统实践. 新舞台新气象, 深化测试基础架构及研发架构,希望能在某个技术领域成为真正的技术大牛。欢迎荐才http://bbs.51testing.com/viewthread.php?tid=120496&extra=&page=1 .邮件: jianzhao.liangjz@alibaba-inc.com,MSN:liangjianzhao@163.com.微博:http://t.sina.com.cn/1674816524

UNITILS数据库测试实例

上一篇 / 下一篇  2009-06-10 20:47:22 / 个人分类:java性能监控与调优

http://www.dbmaintain.org/overview.html

http://www.slideshare.net/nevenfi/unit-testing-unitils-dbmaintain

国外PPT :http://www.parleys.com/display/PARLEYS/Unitils

 

http://www.slideshare.net/nevenfi/unit-testing-unitils-dbmaintain

 

Unitils 2: unitils is dead, long live to spring-test

 

http://203.208.37.132/search?q=cache:qwFKcsUwyLwJ:java-chimaera.blogspot.com/2008/12/unitils-2-unitils-is-dead-long-live-to.html+unitils&cd=43&hl=zh-CN&ct=clnk&gl=cn&st_usg=ALhdy2_5Qbp4RG814rODpTrovRqbSGjXHg

 

http://blog.csdn.net/phphot/archive/2009/03/02/3949340.aspx

http://www.blogjava.net/wangzhouyu/archive/2008/04/07/191137.html

http://code.google.com/p/unitils-idea/

http://rdc.taobao.com/blog/qa/?p=1895

 

http://andyao.javaeye.com/blog/180231  unitils结合dbdeploy测试

 

http://www.devx.com/Java/Article/35129

 

样例代码 :http://www.devx.com/Java/Article/35129/1954

 

Unitils最核心的就是数据库DBxml文件之间互相映射.

Unitils需要下载with-dependency的包下面的例子用spring testcontext也能部分实现访问数据库.

.

有几个刚性要求 :

1)     unitils.properities放在src同级目录

2)     dataset xml文件默认放在和该类同一个目录

3)     @ExpectedDataSet 对应的文件名命名规则及同目录存放

public class UserDAOTest extends UnitilsJUnit4 {

 

   @Test @ExpectedDataSet

   public void testInactivateOldAccounts() {

       userDao.inactivateOldAccounts();

   }

}

Note that we have added the@ExpectedDataSetto the test method. This will instruct Unitils to look for a data set file namedUserDAOTest.testInactivateOldAccounts-result.xmland compare the contents of the database with the contents of the data set:

 

4)     Dbscripts目录下的自动化脚本命令规则

 

The directory in which the scripts are located can be configured with following property:

dbMaintainer.script.locations=myproject/dbscripts

Multiple directories can be specified, separated by commas. Subdirectories are also scanned for script. files. All scripts are required to follow a particular naming pattern: they start with a version number, followed by an underscore and end with '.sql' (the supported extensions are configurable). The example below shows some typical scripts names. The leading zeroes in the example are not required, they are only added for convenience, to make sure they are shown in proper sequence in a file explorer window.

dbscripts/ 001_initial.sql

          002_tracking_updates.sql

          003_auditing_updates.sql

Suppose you add a new script, this time with version number 4:004_create_user_admin_tables.sql. The next time you execute a database test, the database maintainer will notice that the database structure is no longer up to date. It will update the database schema incrementally by executing all of the new scripts, in this case only004_create_user_admin_tables.sql.

 

采用spring2.5+unitils实现一个数据库的测试代码,最后生成的文件目录 :

     

 

上述dboutput文件是第一次执行时由unitils自动导出的.

 

 

工程的Java build pathunitils.jar及依赖的spring,dbunit,mysql引擎都加入.

 

unitils.properities文件内容

 

 

 

# comments documenting these unitils configuration properties removed for

# brevity. look for commenting in unitils-default.properties in the root of the

# unitils jar if needed.

 

unitils.modules=database,dbunit,easymock,spring,inject

 

unitils.module.hibernate.enabled=false

# these placeholders are set in avaje.properties

database.driverClassName=com.mysql.jdbc.Driver

database.url=jdbc:mysql://localhost:3306/amoeba

database.schemaNames=amoeba

database.userName=root

database.password=mysql

database.dialect=mysql

 

DatabaseModule.Transactional.value.default=commit

#DbUnitModule.DataSet.loadStrategy.default=org.unitils.dbunit.datasetloadstrategy.InsertLoadStrategy

 

 

 

# Indicates the database must be recreated from scratch when an already executed script. is updated. If false, the

# DBMaintainer will give an error when an existing script. is updated.

dbMaintainer.fromScratch.enabled=true

# Indicates whether a from scratch update should be performed when the previous update failed, but

# none of the scripts were modified since that last update. If false a new update will be tried only when

# changes were made to the script. files.

dbMaintainer.keepRetryingAfterError.enabled=false

 

updateDataBaseSchema.enabled=true

dbMaintainer.autoCreateExecutedScriptsTable=true

dbMaintainer.useScriptFileLastModificationDates.enabled=true

# list are recursively searched for files.

 

#eclipse must be bin/scripts,or

#Caused by: org.unitils.core.UnitilsException: File location scripts defined in property dbMaintainer.script.locations doesn't exist

 

dbMaintainer.script.locations=bin/resources/dbscripts

# Extension of the files containing the database update scripts

dbMaintainer.script.fileExtensions=sql,ddl

 

dbMaintainer.generateDataSetStructure.enabled=true

dataSetStructureGenerator.xsd.dirName=dboutput/

sequenceUpdater.sequencevalue.lowestacceptable=100

 

 

 

 

 

#dbMaintainer.script.locations这个地方特别小心,在不同的运行ENV可能结果不同.可以下载untils代码一起执行.并且00n代表版本号,建议在手工删除后tabledbmaintain_scripts;可以从001开始,否则sql语句版本从001开始.

 

 

以上选项都是调试出来的,特别注意:
dbMaintainer.fromScratch.enabled=true

updateDataBaseSchema.enabled=true

 

 

 

log4j.properities内容:

log4j.rootLogger=info,CONSOLE

log4j.addivity.org.apache=true

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.Threshold=debug

org.unitils.appender.Threshold=debug

log4j.appender.CONSOLE.Target=System.out

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

log4j.appender.CONSOLE.layout.ConversionPattern=[framework]%d-%c-%-4r[%t]%-5p%c%x-%m%n

 

 

 


TAG: dbunit unitils 自动化

 

评分:0

我来说两句

Open Toolbar