软件测试中的单元测试之Service的测试
作者:网络转载 发布时间:[ 2015/5/28 14:37:45 ] 推荐标签:软件测试
测试Service,因为Service依赖的Dao,所以只需Mock一个Dao即可。在这里我详细的介绍关于注册这个功能的测试
java代码
public interface IAccountService extends IBaseService{
Account findAccountById(String id);
Account findAccounByName(String name);
void regist(Account account)throws ObjectExistsException;
}
注册功能的实现。
java代码
public void regist(Account account)throws ObjectExistsException{
if(accountDao.findAccounByName(account.getName())!=null)
throw new ObjectExistsException("User's name is exists!");
accountDao.save(account);
}
测试代码
java代码
protected void setUp()throws Exception{
control=MockControl.createControl(IAccountDao.class);
accountDao=(IAccountDao)control.getMock();
as=new AccountService();
as.setAccountDao(accountDao);
}
public void testFindAccountByName(){
String name="wuhua";
accountDao.findAccounByName(name);
Account a=new Account("wuhua");
a.setId(name);
control.setReturnValue(a);
control.replay();
Account at=as.findAccounByName(name);
Assert.assertEquals(name,at.getId());
Assert.assertEquals(a,at);
control.verify();
}
首先我们建立一个关键字查询,name="wuhua";
然后调用Dao的方法,
然后自定义返回一个自己预期的对象,
后通过比较这个对象判断结果是否是自己想要的

sales@spasvo.com