action类大概样子如下:
java 代码
- public class GuideLinkGropAction extends BaseAction {
-
- private GuideLinkGroup guideLinkGroup;
-
- private TravelBureauService travelBureauService;
-
- public String getTravelBureauNames() throws Exception {
- List travelBureaulList = travelBureauService
- .findTravelBureaulList(new TravelBureau());
- List travelBureauNamesList = getTravelBureauNameList(travelBureaulList);
- String travelBureauName = guideLinkGroup.getTravelBureau().getUserName();
- responseOut(travelBureauNamesList, travelBureauName);
- return SUCCESS;
- }
-
- private List getTravelBureauNameList(List travelBureaulList) {
- List list = new ArrayList();
- Iterator iter = travelBureaulList.iterator();
- while (iter.hasNext()) {
- TravelBureau travelBureau = (TravelBureau) iter.next();
- CommonInfo info = new CommonInfo();
- info.setId(travelBureau.getId());
- info.setValue(travelBureau.getUserName());
- list.add(info);
- }
- return list;
- }
-
- private void responseOut(List travelBureauNamesList, String travelBureauName)
- throws Exception {
- HttpServletResponse response = ServletActionContext.getResponse();
- List matching = CommonUtil.prefixMatch(travelBureauNamesList,
- travelBureauName);
- PrintWriter out = response.getWriter();
- response.setContentType("text/xml");
- response.setHeader("Cache-Control", "no-cache");
- Iterator iter = matching.iterator();
- while (iter.hasNext()) {
- CommonInfo info = (CommonInfo) iter.next();
- String name = new String(info.getValue().getBytes("UTF-8"),
- "ISO8859-1");
- out.print("<name></name>" + name + "");
- out.println("<id></id>" + info.getId() + "");
- }
- out.println("");
- out.close();
- }
- }
上面的getTravelBureauNames方法是为了一个ajax的功能异步获取数据用的,responseOut方法是把数据用xml格式输出,现在要测试getTravelBureauNames方法,怎么测都没有成功,我想的大概测试代码如下:
java 代码
- public class GuideLinkGropActionTest extends TestCase {
-
- private MockControl control2;
-
- private TravelBureauService travelBureauService;
-
- Map session = new HashMap();
-
- GuideLinkGropAction action = new GuideLinkGropAction();
-
- protected void setUp() throws Exception {
- control2 = MockControl.createControl(TravelBureauService.class);
- travelBureauService= (TravelBureauService) control2.getMock();
- }
-
- public void testGetTravelBureauNames()throws Exception {
- travelBureauService.findTravelBureaulList(new TravelBureau());
- control2.setReturnValue(new ArrayList());
- control2.replay();
-
- action.setTravelBureauService(travelBureauService);
- assertEquals(Action.SUCCESS, action.getTravelBureauNames());
- control2.verify();
- }
- }
这个测试代码肯定是通不过的