DbUnit (å®è£ | 使ç¨)
è¿ç¨DBUnitè¿è¡é«æåå æµè¯
è¯è 注ï¼æè¿å¯¹DBUnitæ¯è¾æå ´è¶£ï¼çå°è¿ç¯æç« å°±ç¿»è¯åºæ¥åå¤§å®¶å ±äº«ï¼ä¸è¿æä¹æ¯New
Handï¼æ以翻è¯ä¸å¥½çå°æ¹è¯·å¤§å®¶ææ£ãæçMSN:zhlihui@hotmail.com,源码飞车刷级源码å¦æ大家æä»ä¹å¥½çèµæºåç»éªæ¬¢è¿åæ交æµ.
å¼å ¥DBUnit
ç°å®ç³»ç»ä¸é常ä¼æä¸äºå ·æå¤é¨ä¾èµæ§ç对象ï¼è¿äºå¯¹è±¡åæ°æ®åºæè å ¶ä»å¯¹è±¡åå¨è¯¸å¤å ³èãå¦ææ们对è¿æ ·ç对象ç¼ååå åç»ä»¶çº§æµè¯çè¯ï¼å¯ä»¥æ³è±¡å°æ¯é常麻ç¦çä¸ä»¶äº.å 为è¿ç§å¤é¨ä¾èµæ§çåå¨ï¼ä½¿çæ们å¾é¾å°å¯¹è±¡å¤ç«åºæ¥è¿è¡æµè¯ãç»å¸¸æåçç½çæµè¯æ³ï¼åºæ¬ä¸å°±æ¯éè¿æ§å¶å¯¹è±¡çå¤é¨ä¾èµæ§æ¥è¾¾å°é离对象çç®çï¼ä½¿çå¯ä»¥æä½è¿äºå¯¹è±¡çç¶æåç¸å ³è¡ä¸ºã
è¿ç¨ 模æ对象(mock objects)
æè stubsï¼å°±æ¯ä¸ä¸ªæ§å¶å¯¹è±¡å¤é¨ä¾èµæ§ç解å³æ¹æ¡ãéè¿é离é£äºå ³èçæ°æ®åºè®¿é®ç±»ï¼è±¡JDBCçç¸å ³æä½ç±»ï¼å¯¹äºæ§å¶å¯¹è±¡å¤é¨ä¾èµæ§å°æ¯å¾ææçãä½æ¨¡æ对象ç解å³æ¹æ¡å¯¹ä¸äºç¹æ®çåºç¨ç³»ç»æ¶æå°±æ¾å¾åä¸ä»å¿äºï¼è±¡é£äºè¿ç¨äºEJBçCMP(container-managed
persistence)æè JDO(java Data
Objects)çåºç¨ç³»ç»æ¶æï¼å¨è¿äºæ¶æéï¼æ°æ®åºç访é®å¯¹è±¡æ¯å¨æåºå±çèä¸å¾éè½ã
ç±Manuel Laflamme
ç¼åçå¼æ¾æºä»£ç çDBUnitæ¶æä½ç³»ï¼å¯¹äºæ§å¶ç³»ç»å é¨çæ°æ®åºä¾èµæ§æä¾äºä¸ä¸ªé常ä¸éç解å³æ¹æ¡ãä»å 许ç¨åºåå¨æ´ä¸ªçæµè¯è¿ç¨ä¸èªç±ç管çæ§å¶æ°æ®åºçç¶æï¼è¿å¾éè¦ãå©ç¨DBUnitï¼å¨æµè¯ä¹åï¼æ们å¯ä»¥ç»ç®æ æ°æ®åºæ¤å ¥æ们éè¦çæ°æ®éï¼èä¸ï¼å¨æµè¯å®æ¯åï¼æ°æ®åºå®å ¨è½å¤å溯å°æµè¯åçç¶æã
å¨å¾å¤æåç软件项ç®ä¸ï¼æµè¯èªå¨åå¾å¾æ¯å ³é®çå±é¢ãDBUnitå 许å¼å人åå建æµè¯ç¨ä¾ä»£ç ï¼å¨è¿äºæµè¯ç¨ä¾ççå½å¨æå æ们å¯ä»¥å¾å¥½çæ§å¶æ°æ®åºçç¶æãèä¸,è¿äºæµè¯ç¨ä¾æ¯å¾å®¹æå®ç°èªå¨åçãè¿æ ·å¨æµè¯è¿ç¨ä¸æ们æ 须对å®è¿è¡äººå·¥çå¹²é¢ï¼ä¸ºäººå·¥å¹²é¢é æçåæèæ å¿å°±æ´æ²¡å¿ è¦äºã
ç®åä»ç»
é 置使ç¨DBUnitç第ä¸æ¥æ们é¦å éè¦ç¥éå¦ä½çææ°æ®åºschemaï¼è¿ä¸ªæ件æ¯XMLæ ¼å¼çï¼å ¶ä¸å æ¬äºæ°æ®åºç表åç¸å ³æ°æ®ä¿¡æ¯ã
ä¾å¦ï¼è¿éæä¸ä¸ªæ°æ®åºè¡¨employee
ï¼æ们å¯ä»¥ç¨SQLçå½¢å¼è¿æ ·å°ä»è¡¨ç¤ºåºæ¥ã
èä¸ï¼æ们å¯ä»¥çå°ï¼ä¸ä¸ªç®åçæ°æ®éå¯ä»¥è¿æ ·è¡¨ç¤º
å¨DBUnitä¸ï¼ä¸é¢è¿ä¸ªè¡¨åæ½æ ·æ°æ®ä¿¡æ¯å¯ä»¥ç¨XMLæ件çå½¢å¼è¿æ ·è¡¨ç¤ºï¼
<EMPLOYEE employee_uid='1'
start_date='--'
first_name='Andrew'
ssn='xxx-xx-xxxx'
last_name='Glover' />
è¿ä¸ªçæçXMLæ ¼å¼çæ件å¯ä»¥ä½ä¸ºç³»ç»æéçææç§åæ件(seed
files)çæ ·æ¬æ¨¡ç.
为ç¸äºå ³èçæµè¯åºæ¯å建å¤ä¸ªç§åæ件æ¯ä¸ä¸ªå¾ææççç¥ï¼å°±è±¡éè¿ä¸åçæ°æ®åºæ件æ¥åºåé离æ°æ®åºç¶ææ¯ä¸ä¸ªéçãå¤ç§åæ件çç¥å¯ä»¥å°æ们çæµè¯ç®æ éå®å°è¾å°çèå´ï¼ç®æ æ°æ®å¯ä»¥åªé对æ°æ®åºç表ï¼èä¸æ¯æ´ä¸ªæ°æ®åºã
为äºç»ç®æ æ°æ®åºæ¤å ¥ä¸åçèåè®°å½ï¼æ们éè¦çXMLæ°æ®æ件å¦ä¸æ示ï¼
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<EMPLOYEE employee_uid='1'
start_date='--'
first_name='Drew' ssn='--'
last_name='Smith' />
<EMPLOYEE employee_uid='2'
start_date='--'
first_name='Nick' ssn='--'
last_name='Marquiss' />
<EMPLOYEE employee_uid='3'
start_date='--'
first_name='Jose' ssn='--'
last_name='Whitson' />
</dataset>
ç°å¨ï¼è¦è®©DBUnitåæ们æéçæ°æ®åºschemaä¸èµ·å·¥ä½äºï¼å¯¹äºç¨åºåæ¥è¯´ï¼æ们使ç¨DBUnitè¿è¡æµè¯å¯ä»¥æ两ç§éæ©ï¼éè¿ç´æ¥ç¼ç æ¹å¼è¿è¡æµè¯æè ä¸Antç»å.
ç¼ç æ¹å¼
DBUnitæ¡æ¶æä¾äºä¸ä¸ªåºæ¬çæ½è±¡æµè¯ç¨ä¾ç±»ï¼å«åDatabaseTestCaseï¼å®æ¯JUnitæ¡æ¶ä¸çåºç¡ç±»TestCaseçåç±»ãå¦ææ们使ç¨è¿ä¸ªç±»å¿ é¡»é¦å å®ç°ä¸¤ä¸ªé©åæ¹æ³(hook
methods)ï¼getConnection()ågetDataSet().
æ¹æ³getConnection()éè¦è¿åä¸ä¸ªIDatabaseConnectionç±»åç对象ï¼è¿ä¸ªå¯¹è±¡æ¯ä¸ä¸ªåºäºæ®éJDBCè¿æ¥çå è£ ç±»ãä¾å¦ï¼ä¸é¢ç代ç 段æ¼ç¤ºäºå¨MySQLæ°æ®åºç¯å¢ä¸ï¼IDatabaseConnectionç±»åè¿æ¥å¯¹è±¡çå建æ¹æ³ã
protected IDatabaseConnection getConnection()
throws Exception {
Class driverClass = Class.forName("org.gjt.mm.mysql.Driver");
Connection jdbcConnection = DriverManager.getConnection(
"jdbc:mysql://.0.0.1/hr", "hr", "hr");
return new DatabaseConnection(jdbcConnection);
}
æ¹æ³getDataSet()è¿åä¸ä¸ªIDataSetç±»å对象ï¼å ¶å®ï¼è¯´ç½äºï¼ä»å°±æ¯æ们å åæå°çXMLæ°æ®çç§åæ件çå¦ä¸ç§è¡¨ç°å½¢å¼ã
protected IDataSet getDataSet() throws Exception {
return new FlatXmlDataSet(
new
FileInputStream("hr-seed.xml"));
}
æäºè¿ä¸¤ä¸ªåºæ¬çæ¹æ³ä»¥åï¼DBUnitå°±å¯ä»¥æç §å®é¢å 缺ççè¡ä¸ºå·¥ä½äºãDatabaseTestCaseç±»æä¾äºä¸¤ä¸ªfixtureï¼æå«å®åºä»¶ï¼ä¸ç¥ä»å åæå¦ï¼ï¼æ¹æ³æ¥æ§å¶æµè¯ååæµè¯åçæ°æ®åºç¶æãè¿ä¸¤ä¸ªæ¹æ³å°±æ¯ï¼
getSetUpOperation() å getTearDownOperation().
ä¸ç§é«æçå®æ½æ¹æ¡å°±æ¯è®©getSetUpOperation()æ¹æ³æ§è¡REFRESHæä½ï¼éè¿è¿ä¸ªæä½,æ们å¯ä»¥ç¨ç§åæ件ä¸çæ°æ®å»æ´æ°ç®æ æ°æ®åºéçæ°æ®ãæ¥ä¸æ¥ï¼å°±æ¯getTearDownOperation()ï¼è®©ä»å»æ§è¡ä¸ä¸ªNONEæä½ï¼ä¹å°±æ¯ä»ä¹ä¹ä¸æ§è¡ã
protected DatabaseOperation getSetUpOperation()
throws
Exception {
return DatabaseOperation.REFRESH;
}
protected DatabaseOperation getTearDownOperation()
throws
Exception {
return DatabaseOperation.NONE;
}
è¿æä¸ç§ææçæ¹æ³å°±æ¯å¨getSetUpOperation()æ¹æ³ä¸æ§è¡CLEAN_INSERTæä½ï¼è¿æ ·é¦å ä¼å°ç®æ æ°æ®åºä¸ä¸æ们æä¾çç§åæ件ä¸è´çæ°æ®å é¤ï¼ç¶åå°æ们æä¾çæ°æ®æå ¥å°æ°æ®åºä¸ãè¿ä¸ªå®æ½é¡ºåºä¿è¯äºæ们对æ°æ®åºç精确æ§å¶ã
代ç æ ·ä¾
å¨ä¸ä¸ªåºäºJ2EEç人åèµæºç³»ç»ä¸ï¼æ们å¾å¸æ对æ个æ°æ®æä½å¨æå®ç°æµè¯èªå¨å,è¿ä¸ªæä½å¨æå æ¬èåçæ°å¢ï¼æ£ç´¢ï¼æ´æ°åå é¤ãè¿ç¨æ¥å£å®ä¹äºä¸åçä¸å¡æ¹æ³ï¼ä¸ºäºç®æ´æ¸ æ¥ï¼çç¥äºæ¹æ³ä¸çthrowsåå¥ï¼.
//è¯è 注ï¼è¿éçEmployeeValueObjectç±»å对象ï¼è¯è 认为æ¯ä»£è¡¨èåå®ä½ä¿¡æ¯ç对象ã
public void createEmployee( EmployeeValueObject emplVo )
public EmployeeValueObject getEmployeeBySocialSecNum( String ssn )
public void updateEmployee( EmployeeValueObject emplVo )
public void deleteEmployee( EmployeeValueObject emplVo )
æµè¯getEmployeeBySocialSecNum()æ¹æ³
éè¦æ¤å ¥ä¸æ¡æ°æ®å°ç®æ æ°æ®åºä¸ï¼å¦å¤ï¼æµè¯deleteEmployee()æ¹æ³åupdateEmployee()æ¹æ³æ¶ï¼åæ ·ä¹æ¯å¨å åæ¤å ¥çè¿æ¡è®°å½çåºç¡ä¸è¿è¡ãæåï¼æµè¯ç±»ä¼é¦å å©ç¨createEmployee()æ¹æ³å建ä¸æ¡è®°å½ï¼åæ¶æ们éè¦æ ¡éªæ§è¡è¿ä¸ªæ¹æ³æ¶ï¼æ¯å¦ä¼æå¼å¸¸åçã
ä¸é¢è¿ä¸ªDBUnitç§åæ件ï¼å«å"employee_hr_seed.xml",ä¸é¢å°ç¨å°è¿ä¸ªæ件ã
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<EMPLOYEE employee_uid='1'
start_date='--'
first_name='Drew' ssn='--'
last_name='Smith' />
<EMPLOYEE employee_uid='2'
start_date='--'
first_name='Nick' ssn='--'
last_name='Marquiss' />
<EMPLOYEE employee_uid='3'
start_date='--'
first_name='Jose' ssn='--'
last_name='Whitson' />
</dataset>
æµè¯ç±» EmployeeSessionFacadeTest
,éè¦æ©å±DBUnitçåºç¡ç±»DatabaseTestCase并ä¸å¿ é¡»æä¾å¯¹getConnection()ågetDataSet()æ¹æ³çå®ç°ï¼å¨getConnection()æ¹æ³ä¸å°è·å¾ä¸EJB容å¨åå§åæ¶ä¸æ ·çæ°æ®åºå®ä¾ï¼getDataSet()æ¹æ³è´è´£è¯»åä¸é¢æåçemployee_hr_seed.xmlæ件çæ°æ®ã
æµè¯æ¹æ³ç¸å½ç®åï¼å 为DBUnitå·²ç»ä¸ºæ们å¤çäºå¤æçæ°æ®åºçå½å¨æä»»å¡ã为äºæµè¯getEmployeeBySocialSecNum()æ¹æ³ï¼åªéè¦ç®åçä¼ éä¸ä¸ªåå¨äºç§åæ件ä¸ç社ä¿ä»£ç å·å³å¯,æ¯å¦
"--".
//è¯è 注ï¼EmployeeFacade ç±»å对象ï¼è¯è 认为æ¯ä»£è¡¨åºå±æ°æ®åºæ°æ®çæ å°ä½
public void testFindBySSN() throws Exception{
EmployeeFacade facade = //obtain somehow
EmployeeValueObject vo =
facade.getEmployeeBySocialSecNum("--");
TestCase.assertNotNull("vo shouldn't be null", vo);
TestCase.assertEquals("should be Drew",
"Drew", vo.getFirstName());
TestCase.assertEquals("should be Smith",
"Smith", vo.getLastName());
}
为äºç¡®ä¿æä½å¨æä¸çå建èåæ¹æ³createEmployee()没æé®é¢ï¼æ们åªéç®åçæ§è¡ä¸ä¸è¿ä¸ªæ¹æ³ï¼ç¶åæ ¡éªä¸ä¸çæ没æå¼å¸¸æåºï¼å¦å¤ï¼ä¸ä¸æ¥æ们è¦åçå°±æ¯å¨è¿æ¡æ°å¢çè®°å½ä¸è¿è¡æ¥æ¾æä½ï¼çæ¯å¦å¯ä»¥æ¾å°åå建çè®°å½ã
public void testEmployeeCreate() throws Exception{
EmployeeValueObject empVo = new EmployeeValueObject();
empVo.setFirstName("Noah");
empVo.setLastName("Awan");
empVo.setSSN("--");
EmployeeFacade empFacade = //obtain from somewhere
empFacade.createEmployee(empVo);
//perform a find by ssn to ensure existence
}
æµè¯updateEmployee()æ¹æ³å æ¬åæ¥ï¼é¦å æ¥æ¾æ们éè¦è¢«æ´æ°çé£æ¡è®°å½ï¼ç¶åæ´æ°å®ï¼ç´§æ¥çï¼éæ°æ¥æ¾è¿æ¡è®°å½ï¼ç¡®è®¤æ´æ°æä½æ¯å¦ææã
public void testUpdateEmployee() throws Exception{
EmployeeFacade facade =//obtain facade
EmployeeValueObject vo =
facade.getEmployeeBySocialSecNum("--");
TestCase.assertNotNull("vo was null", vo);
TestCase.assertEquals("first name should be Jose", "Jose",
vo.getFirstName());
vo.setFirstName("Ramon");
facade.updateEmployee(vo);
EmployeeValueObject newVo =
facade.getEmployeeBySocialSecNum("--");
TestCase.assertNotNull("vo was null", newVo);
TestCase.assertEquals("name should be Ramon", "Ramon",
newVo.getFirstName());
}
ç¡®ä¿æ°æ®æä½å¨æä¸çå é¤æä½deleteEmployee()çæ¹æ³åtestUpdateEmployee()æ¹æ³åºæ¬ç±»ä¼¼ã
å®å为ä¸æ¥ï¼é¦å æ¥æ¾ä¸ä¸ªå·²åå¨çè®°å½å®ä½ï¼ç¶å移é¤ï¼æåå对ç¸åçè®°å½è¿è¡æ¥æ¾ï¼ç¡®è®¤è¿æ¡è®°å½æ²¡æ被æ¥å°ã
public void testDeleteEmployee() throws Exception{
EmployeeFacade facade = //obtain facade
EmployeeValueObject vo = facade.getEmployeeBySocialSecNum("--");
TestCase.assertNotNull("vo was null", vo);
facade.deleteEmployee(vo);
try{
EmployeeValueObject newVo =
facade.getEmployeeBySocialSecNum("--");
TestCase.fail("returned removed employee");
}catch(Exception e){
//ignore
}
}
ä¸è¿°è¿äºæµè¯ä»£ç å¾ç®åä¹å¾å®¹æç解.å 为è¿äºä»£ç å¯ä¸çè责就æ¯æµè¯ï¼å·²ç»å®å ¨ä»ç³»ç»ç¨åºä»£ç ä¸ç¬ç«åºæ¥ï¼è¿ä½¿æµè¯åçç®åã并ä¸ï¼è¿äºæµè¯ç¨ä¾çèªå¨åä¹å¾å®¹æå®ç°ã
ä¸Antçç»å
ç¸å¯¹äºæ©å±DBUnitä¸çåºç¡ç±»DatabaseTestCase,DBUnitæ¡æ¶ä¸èªå¸¦Antåè½,å 许æ们å¯ä»¥å¨Antçbuild.xmlæ件ä¸æ§å¶æ°æ®åºçç¶æ.è¿ä¸ªåè½æ¯ç¸å½å¼ºå¤§çï¼å 为对äºä½æç诸å¤æµè¯ç¨ä¾ï¼å®æä¾äºä¸ä¸ªç¸å½ç®æ´ç解å³æ¹æ¡ãæ¯å¦ãå¨Antä¸è¿è¡JUnitæµè¯,就象ä¸é¢å®ä¹ä¸ä¸ªä»»å¡ä¸æ ·ç®åæäºã
<junit printsummary="yes" haltonfailure="yes">
<formatter type="xml"/>
<batchtest fork="yes"
todir="${ reports.tests}">
<fileset dir="${ src.tests}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
DBUnitä»»å¡è¿ç¨ä¸ï¼ä¸ºäºå¨Junitä»»å¡ååæ§å¶æ°æ®åºçç¶æï¼æ们éè¦å建ä¸ä¸ª"setup"æä½ï¼å¨è¿ä¸ªæä½ä¸ç§åæ件ä¸çæ°æ®å 容ä¼è¢«æå ¥çæ°æ®åºä¸ã
<taskdef name="dbunit"
classname="org.dbunit.ant.DbUnitTask"/>
<dbunit driver=" org.gjt.mm.mysql.Driver "
url=" jdbc:mysql://.0.0.1/hr "
userid="hr"
password="hr">
<operation type="INSERT"
src="seedFile.xml"/>
</dbunit>
ç¶åï¼è¿éè¦ä¸ä¸ª"tear
down"æä½,å¨è¿ä¸ªæä½ä¸ï¼"setup"æä½æå ¥çè®°å½è¢«ä»ç®æ æ°æ®åºä¸å é¤äºã
<dbunit driver=" org.gjt.mm.mysql.Driver "
url=" jdbc:mysql://.0.0.1/hr "
userid="hr"
password="hr">
<operation type="DELETE"
src="seedFile.xml"/>
</dbunit>
ç¨ä¸é¢ç代ç æ¥å è£ JUnitä»»å¡ï¼è½å¤å¨æ¹éæµè¯åææçè£ è½½æ°æ®å°ç®æ æ°æ®åºä¸ï¼å¹¶ä¸å¨æµè¯ç»æåï¼å°å·²è£ è½½çå ¨é¨æ°æ®å é¤ã
<taskdef name="dbunit"
classname="org.dbunit.ant.DbUnitTask"/>
<!-- set up operation -->
<dbunit driver=" org.gjt.mm.mysql.Driver "
url=" jdbc:mysql://.0.0.1/hr "
userid="hr"
password="hr">
<operation type="INSERT"
src="seedFile.xml"/>
</dbunit>
<!-- run all tests in the source tree -->
<junit printsummary="yes" haltonfailure="yes">
<formatter type="xml"/>
<batchtest fork="yes" todir="${ reports.tests}">
<fileset dir="${ src.tests}">
<include name="**/*Test*.java"/>
</fileset>
</batchtest>
</junit>
<!-- tear down operation -->
<dbunit driver=" org.gjt.mm.mysql.Driver "
url=" jdbc:mysql://.0.0.1/hr "
userid="hr"
password="hr">
<operation type="DELETE"
src="seedFile.xml"/>
</dbunit>
ç»è®º
è½å¤å¨æµè¯å¨æå 管çæ°æ®åºçç¶æï¼DBUnitæ¡æ¶çè¿ä¸ªåè½ç¹æ§ä½¿å¾æµè¯ç¨ä¾ä»£ç çå建ååºç¨çå¨æ大大缩çãèä¸ï¼éè¿æ§å¶æ°æ®åºè¿ä¸ªä¸»è¦çä¾èµå¯¹è±¡ï¼ä½¿çå©ç¨DBUnitæ¡æ¶ççæµè¯æ´å®¹æèªå¨åã
DBUnitç²¾å¦ç设计ï¼ä½¿çå¦ä¹ 使ç¨å®åå¾å¾ç®åãå¨ä½ çæµè¯æ¹æ¡ä¸ï¼å¦æä½ è½å¤æ£ç¡®ç使ç¨å®ï¼é£ä¹å¸¦æ¥çå°æ¯ä»£ç 稳å®æ§æ¹é¢çå¤§å¹ åº¦å¢å¼ºï¼å½ç¶è¿ä¼ä½¿ä½ çå¼åå¢éä¿¡å¿åå¢ã
2024-11-14 13:00
2024-11-14 12:39
2024-11-14 11:39
2024-11-14 11:30
2024-11-14 11:21