用HTTPUNIT自动测试

HttpUnit虽然也带着个unit,好象应属于于功能测试那一类吧。这儿有一个例子,是测试一个login功能的。如果真要是这么测试,总感觉有点“****,自找麻烦”。^_^

----LoginTest.java-------
package com.hedong.httpunit.test;
import com.meterware.httpunit.*;
import junit.framework.*;
import java.util.*;
import java.net.URL;
import java.net.MalformedURLException;
import org.xml.sax.SAXException;
public class LoginTest extends TestCase{
        WebConversation conversation;
        public DangjianLoginTest(String name){
                super(name);
        };
        public void setUp(){
                conversation = new WebConversation();
        }
        public static Test suite(){
                return new TestSuite(LoginTest.class);
        }
        private WebResponse goToPrivatePage() throws Exception{
                return conversation.getResponse("http://yourhost.com/test/private.jsp");
        }
        public void testLogin() throws Exception{(www.iocblog.net 文章来源)
         //直接访问private.jsp会要求登录
                WebResponse response = this.goToPrivatePage();
                assertLoginPage(response);
                //用户、密码输入错误,要求登录
                response=login(response,"xxx","no password");
                assertLoginPage(response);
                //用户、密码输入正确,不要求登录
                response=login(response,"a","b");
                assertTrue(!response.getTitle().equals("Login"));
        }
        public void assertLoginPage(WebResponse response) throws Exception{
         //为简便,此处用页面的标题来判断是哪个页面。
                System.out.println(response.getText());
                assertEquals("Login",response.getTitle());
        }
        public WebResponse login(WebResponse loginPage, String userName, String pass) throws Exception{
                WebForm form=loginPage.getForms()[0];//取得第一个form
                WebRequest loginRequest=form.getRequest();
                loginRequest.setParameter("userName",userName);
                loginRequest.setParameter("password",pass);
                return conversation.getResponse(loginRequest);
        }
}
-------login.jsp-----
<html><head><title>Login</title></head>
<body><form action="chkLogin.jsp">
<input type="text" name="userName" value="">
<input type="password" name="password" value="">
<input type="submit" name="submit" value="submit!">
</form></body>
------chkLogin.jsp----
<%
     String UserID,UserPW;
     UserID=request.getParameter("userName");
     UserPW=request.getParameter("password");
     if (UserID.equals("a") && UserPW.equals("b")) {
        session.setAttribute("userid",UserID);
        %>
        <jsp:forward page="private.jsp" />
        <%
     } else {
     %>
     <jsp:forward page="login.jsp" />
     <%
     }
%>
------private.jsp-----
<%
String user=(String)session.getAttribute("userid");
if (user==null){
   %><jsp:forward page="login.jsp" /><%
}
%>
<html><head><title>Private</title></head>
<body>:)</body></html>                                            (www.iocblog.net 文章来源)




文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。