单元测试之实践三 Service的测试
测试Service,因为Service依赖的Dao, 所以只需Mock一个Dao即可。在这里我详细的介绍关于注册这个功能的测试
public interface IAccountService extends IBaseService {
Account findAccountById(String id);
Account findAccounByName(String name);
void regist(Account account) throws ObjectExistsException;
} 注册功能的实现。
public void regist(Account account) throws ObjectExistsException {
if(accountDao.findAccounByName(account.getName()) != null)
throw new ObjectExistsException("User's name is exists!");
accountDao.save(account);
} 测试代码
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的方法,
然后自定义返回一个自己预期的对象,
最后通过比较这个对象判断结果是否是自己想要的
相关推荐
snowphy 2020-05-12
蛰脚踝的天蝎 2020-11-10
Cocolada 2020-11-12
TuxedoLinux 2020-09-11
snowphy 2020-08-19
83540690 2020-08-16
lustdevil 2020-08-03
83417807 2020-07-19
张文倩数据库学生 2020-07-19
bobljm 2020-07-07
83417807 2020-06-28
86427019 2020-06-28
86427019 2020-06-25
zhengzf0 2020-06-21
tobecrazy 2020-06-16
宿命java 2020-06-15
83417807 2020-06-15
84284855 2020-06-11