|
使用Cactus+Jetty测试Servlet
环境:
1. jetty-5.1.8-all.zip
2. JDK1.4.2
(1) 建立LoginServletTestJetty类,用于测试LoginServlet:
package test;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.apache.cactus.extension.jetty.JettyTestSetup;
public class LoginServletTestJetty extends ServletTestCase {
public static Test suite() {
System.setProperty("cactus.contextURL", "http://localhost:8080/test");
TestSuite suite = new TestSuite();
suite.addTestSuite(LoginServletTestJetty.class);
return new JettyTestSetup(suite);
}
public void beginIsValideUser(WebRequest webRequest) {
webRequest.addParameter("name", "cw");
webRequest.addParameter("password", "123");
}
public void testIsValideUser() {
LoginServlet loginServlet = new LoginServlet();
assertTrue(loginServlet.isValideUser(request));
}
public static void main(String args[]) {
TestRunner.run(LoginServletTestJetty.suite());
}
}
(2) 撰写一待测试的LoginServlet:
package test;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
public boolean isValideUser(HttpServletRequest request) {
String name = request.getParameter("name");
String password = request.getParameter("password");
if (name == null || password == null || !name.equals("cw")
|| !password.equals("123")) {
return false;
}
return true;
}
}
运行JUNIT测试 |
|