(这个配置文件中预留了未来使用 sequence 的可能)
DBUnit配置文件
我们通过一个xml种子文件(seedfile)为DBUnit提供测试数据,文件中的数据会被DBUnit在测试开始前自动植入数据表,这个文件结构很简单:
dataSet.xml
<?xml version='1.0' encoding='UTF-8'?>
<DATASET>
<ACCOUNT id='1'
username='Drew'
password='Smith'/>
<ACCOUNT id='2'
username='Nick'
password='Marquiss'/>
<ACCOUNT id='3'
username='Jose'
password='Whitson'/>
</DATASET>
“ACCOUNT”是表名称,它的属性是字段内容。
代码
辅助类Accout.java
package com.wang.dbunit;
public class Account
{
private boolean generateIdFromSequence=false;
private boolean includePassword = false;
private long id = 0;
private String userName = null;
private String password = null;
public boolean getGenerateIdFromSequence()
{
return generateIdFromSequence;
}
public void setGenerateIdFromSequence(boolean generateIdFromSequence)
{
this.generateIdFromSequence =generateIdFromSequence;
}
public void setId(long id)
{
this.id =id;
}
public long getId()
{
return this.id;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password =password;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName =userName;
}
public boolean isIncludePassword()
{
return includePassword;
}
public void setIncludePassword(boolean includePassword)
{
this.includePassword =includePassword;
}
}
业务类AccountManager.java
package com.wang.dbunit;
import com.ibatis.sqlmap.client.SqlMapClient;
public class AccountManager
{
protected SqlMapClient sqlMap = null;
public void setSqlMapClient(SqlMapClient sqlMapClient)
{
this.sqlMap =sqlMapClient;
}
public Account getAccount(String userName, String password, boolean includePassword) throws Exception
{
Account account = new Account();
account.setUserName(userName);
account.setPassword(password);
account.setIncludePassword(includePassword);
Account ret
= (Account)(sqlMap.queryForObject("getAccount", account));
return ret;
}
public void createAccount(String userName, String password) throws Exception
{
Account account = new Account();
account.setUserName(userName);
account.setPassword(password);
sqlMap.insert("createAccount",account);
}
}
好了,我们完成了了全部测试环境,接下来我们要开始编写测试用例。