xuanfeng6060
2015-04-24 02:14
采纳率: 100%
浏览 1.8k
已采纳

求struts1 中测试action类中的方法?

/** 测试类*/
public class TestInfoActionCreateSHTML extends MockStrutsTestCase{
private static ApplicationContext context;

@Override
public void setUp() throws Exception {
    super.setUp();
    System.out.println("执行 setUp()");
    String[] locations = { "/WEB-INF/applicationContext.xml",
            "/WEB-INF/actionServlets.xml" };
    context = new ClassPathXmlApplicationContext(locations);
    setConfigFile("teacher", "/WEB-INF/applicationContext.xml");
}

@Override
public void tearDown() {
    System.out.println("执行 teardown()");
}

@Test
public void testCreateShtml() throws IOException, TemplateException{
/** 要测试InfoAction中的createSHTML(mapping, form, request, response);
方法,那参数怎么传递?
    注:InfoAction 有多个方法,配置路径:“/info”,createSHTML()方法的返回值为null,主要测试该方法是否成功生成shtml文件
*/

}

}

/** action 类*/
public ActionForward createAllSHTML(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws TemplateModelException, IOException {

    Map<String, Object> data = new HashMap<String, Object>();
    JSONObject jsonObj = new JSONObject();
    IInfoBIZ infoBIZ = (IInfoBIZ) getBean("infoBIZ");
    int lxid = request.getParameter("lxid") == null ? 0 : Integer
            .parseInt(request.getParameter("lxid").toString());

    IInfoTypeBIZ infoTypeBIZ = (IInfoTypeBIZ) getBean("infoTypeBIZ");
    Txxlx txxlx = infoTypeBIZ.getInfoByID(lxid);
    List<Map<String, String>> pathList = new ArrayList<Map<String, String>>();
    getWebPath(txxlx, infoTypeBIZ, pathList);

    List list = infoBIZ.findAll3(lxid);
    for (int i = 0; i < list.size(); i++) {
        Txxfb txxfb = (Txxfb) list.get(i);
        InfoForm infoForm = (InfoForm) this.getFvHelper().convertV2F(txxfb);

        //设置参数
        data.put("infoForm" + txxfb.getXxid(), infoForm);
        data.put("pathList", pathList);//网页路径

        // 设置生成SHTML路径
        String shtmlPath0=request.getRealPath("")+"/";
        String shtmlPath =shtmlPath0 + txxlx.getLxml()
                + "/info/infoname" + txxfb.getXxid() + ".shtml";
        // 设置模板路径
        String templatePath = "templat/data.html";
        try {
            FreeMarkerUtil.createSHTML(request.getSession()
                    .getServletContext(), data, templatePath, shtmlPath);
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }

    jsonObj.put("validateFlag", "1"); // 用这个判断是否要刷新
    jsonObj.put("message", "成功生成" + list.size() + "个SHTML页面");

    try {
        response.getWriter().print(jsonObj);
    } catch (IOException e1) {
        System.out.println(e1.getMessage());
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return null;
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题