以下是一个struts测试的完整源代码
import servletunit.struts.MockStrutsTestCase;
public class TestLoginAction extends MockStrutsTestCase ...{
public TestLoginAction(String testName) ...{
super(testName);
}
public void setUp() throws Exception ...{
super.setUp();
setInitParameter("validating","false");
}
public void testSuccessfulLogin() ...{
addRequestParameter("username","deryl");
addRequestParameter("password","radar");
setRequestPathInfo("/login");
actionPerform();
verifyForward("success");
verifyForwardPath("/main/success.jsp");
assertEquals("deryl",getSession().getAttribute("authentication"));
verifyNoActionErrors();
}
public void testFailedLogin() ...{
addRequestParameter("username","deryl");
addRequestParameter("password","express");
setRequestPathInfo("/login");
actionPerform();
verifyForward("login");
verifyForwardPath("/login/login.jsp");
verifyInputForward();
verifyActionErrors(new String[] ...{"error.password.mismatch"});
assertNull(getSession().getAttribute("authentication"));
}
public static void main(String[] args) ...{
junit.textui.TestRunner.run(TestLoginAction.class);
}
}如果要对struts进行单元测试需要用到第三方软件包
本文提供了一个使用StrutsTestCase库进行Struts登录功能单元测试的示例代码。通过两个测试用例验证了成功和失败的登录场景,展示了如何设置请求参数、路径信息以及验证返回结果。

264

被折叠的 条评论
为什么被折叠?



