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最核心的就是数据库DB和xml文件之间互相映射.
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 path把unitils.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代表版本号,建议在手工删除后table及dbmaintain_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