public ProClassGlAction(){
try {
manage=new ProClassManageImpl();
} catch (SQLException e) {
e.printStackTrace();
}
}
public String index()
{
try {
List<ProClass> pclist = manage.getAll();
//以下语句依赖容器,不方便测试,所以我们通过注入request对象
//Map request = (Map) ActionContext.getContext().get("request");
request.put("pclist",pclist );
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
public String addGet(){
return SUCCESS;
}
public String addPost(){
try {
manage.add(proClass);
} catch (SQLException e) {
request.put("error", "插入产品类型失败!");
return "fail";//继续填写信息
}
return SUCCESS;
}
public String editGet(){
proClass=manage.getSimple(proClass.getProClassId());
return SUCCESS;
}
public String editPost()
{
try {
manage.update(proClass);
} catch (SQLException e) {
// TODO Auto-generated catch block
request.put("error", "更新数据失败!");
return INPUT;//继续编辑
}
return SUCCESS;
}
public String deleteGet()
{
try {
manage.delete(proClass.getProClassId());
} catch (Exception e) {
request.put("error", "删除数据失败,可能有相联的数据");
return INPUT;//跳转到失败页
}
return SUCCESS;
}
@Test
public void testIndex() throws Exception {
//准备服务层返回的数据
List<ProClass> list=new ArrayList<ProClass>();
list.add(new ProClass(1,"数据1"));
list.add(new ProClass(2,"数据2"));
//创建一个ProClassManage的模拟对象
ProClassManage manage=createMock(ProClassManage.class);
//模拟manage对象中getAll()方法返回的值
expect(manage.getAll()).andReturn(list);
replay(manage);//回放
action.setManage(manage);//注入manage对象
//注入Map的request
Map request=new HashMap();
action.setRequest(request);
String result=action.index();//执行被测类
//断言返回值
assertEquals("success", result);
assertEquals(2, ((List)request.get("pclist")).size());
}
@Test
//@Ignore
public void testDeleteGet() throws Exception {
//准备删除的数据
ProClass proclass=new ProClass(101,"");
action.proClass=proclass;
//创建一个ProClassManage的模拟对象
ProClassManage manage=createMock(ProClassManage.class);
manage.delete(101);
replay(manage);//回放
action.setManage(manage);
String result=action.deleteGet();
//返回字符串进行断言
assertEquals("success", result);
}
//删除失败
@Test
//@Ignore
public void testDeleteGet1() throws Exception {
//准备删除的数据
ProClass proclass=new ProClass(101,"");
action.proClass=proclass;
//创建一个ProClassManage的模拟对象
ProClassManage manage=createMock(ProClassManage.class);
manage.delete(101); //执行服务层的删除操作
//模拟抛出异常
expectLastCall().andThrow(new IllegalArgumentException());
replay(manage);//回放
action.setManage(manage);//注入manage对象
//注入request对象
Map request=new HashMap();
action.setRequest(request);
String result=action.deleteGet();
//返回字符串进行断言
assertEquals("input", result);
assertEquals("删除数据失败,可能有相联的数据",request.get("error").toString());
}
@Test
public void testEditGet() throws Exception {
//准备数据
ProClass proclass=new ProClass(101,"测试数据");
action.proClass=new ProClass(101,"");
ProClassManage manage=createMock(ProClassManage.class);
expect(manage.getSimple(101)).andReturn(proclass);
replay(manage);//回放
action.setManage(manage);
String result=action.editGet();
//返回字符串进行断言
assertEquals("success", result);
assertEquals("测试数据", action.proClass.getProClassName());
verify(manage);//或者EasyMock.verify(manage);
}
这道题的用意。