【问题标题】:Writing Junit test cases for Struts为 Struts 编写 Junit 测试用例
【发布时间】:2011-11-21 10:14:56
【问题描述】:

我可以为 struts 动作类编写测试用例吗?也就是说,我想测试现有 struts 动作类的执行方法,在这里我可以检查返回的 ActionForward 是什么以及在执行方法中设置的表单内容。举个例子会很有帮助。

谢谢 卡马尔

【问题讨论】:

    标签: java junit struts


    【解决方案1】:

    据我所知,只有StrutsTestCase。但这是针对 JUnit 3,而不是 JUnit 4,所以它似乎已经过时了。

    您从How perform Junit tests with Struts - Ibatis 获得的其他替代品,建议HttpUnit(又很老)或Cactus(已退役)。

    如果必须,我会从StrutsTestCase 开始。这是该站点的示例测试用例:

    public class TestLoginAction extends MockStrutsTestCase {
        public TestLoginAction(String testName) { super(testName); }
    
        public void testSuccessfulLogin() {
           setRequestPathInfo("/login");
           addRequestParameter("username","deryl");
           addRequestParameter("password","radar");
           actionPerform();
           verifyForward("success");
        }
    }
    

    我很久以前就使用过StrutsTestCase,据我所知,它运行良好。

    【讨论】:

    • 感谢 Matthew,StrutsTestCase 非常有帮助。
    • 嗨@matthew-farwell,你知道要测试从ActionForm 到Action 的转换吗?我使用了 setActionForm(myForm),然后调用 actionPerform()。但它似乎无法正常工作。你知道 ActionForm 单元测试的任何样本吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多