AspectJ 和模仿对象的测试灵活性:用“test-only”行为增强单元测试
2010-03-18 00:00:00 来源:WEB开发网客户机代码名为 ClientBean ,它本质上将公开相同的方法,将实现这些方法的任务交给 CustomerManager ,如清单 2 所示。
清单 2. EJB 客户机代码
public class ClientBean {
private Context initialContext;
private CustomerManager manager;
/**
* Includes standard code for referencing an EJB.
*/
public ClientBean() throws Exception{
initialContext = new InitialContext();
Object obj =
initialContext.lookup("java:comp/env/ejb/CustomerManager");
CustomerManagerHome managerHome = (CustomerManagerHome)obj;
/*Resin uses Burlap instead of RMI-IIOP as its default
* network protocol so the usual RMI cast is omitted.
* Mock Objects survive the cast just fine.
*/
manager = managerHome.create();
}
public String[] getCustomers(int ageInYears) throws Exception{
return manager.getCustomersOver(ageInYears);
}
public boolean register(String name) {
try{
manager.register(name);
return true;
}
catch(Exception e){
return false;
}
}
}
更多精彩
赞助商链接