Cactus借助Jetty测试Servlet

这是一个WebProject,但不需要web.xml,因为用不到它


 

首先是待测试的LoginServlet.java

 

package com.jadyer.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 6655227641354075528L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.println("<table><tr><td>username</td><td>password</td></tr></table>");
		out.flush();
		out.close();
	}
	
	public boolean login(HttpServletRequest request){
		String loginUser = (String)request.getSession().getAttribute("loginUser");
		if(null==loginUser || !"http://blog.csdn.net/jadyer".equals(loginUser)){
			System.out.println("用户[" + loginUser + "]登录失败");
			return false;
		}
		System.out.println("用户[" + loginUser + "]登录成功");
		return true;
	}
}


下面是Cactus编写的位于test SourceFolder下的测试用例LoginServletTest.java

 

其实它和上一篇文章"Cactus入门"里面的测试用例是一样的

 

package com.jadyer.servlet;

import java.io.IOException;

import javax.servlet.ServletException;

import junit.framework.Assert;

import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.xml.sax.SAXException;

import com.meterware.httpunit.WebResponse;

public class LoginServletTest extends ServletTestCase {
	private LoginServlet loginServlet;
	
	//它是在ServerSide执行的
	public void setUp(){
		loginServlet = new LoginServlet();
	}
	
	//beginXxx()方法是在ClientSide执行的
	//如果想在testLogin()中获取到这里request添加的username参数,那么这个方法就应该命名为beginLogin()
	public void beginLogin(WebRequest request){
		request.addParameter("username", "Jadyer");
	}
	
	//它是在ServerSide执行的
	public void testLogin(){
		//获取GET参数
		Assert.assertEquals(request.getParameter("username"), "Jadyer");
		//登录失败
		//session.setAttribute("loginUser", "玄玉<http://blog.csdn.net/jadyer>");
		Assert.assertFalse(loginServlet.login(request));
		//登录成功
		session.setAttribute("loginUser", "http://blog.csdn.net/jadyer");
		Assert.assertTrue(loginServlet.login(request));
	}
	
	//它是在ServerSide执行的
	public void testDoGet() throws ServletException, IOException {
		loginServlet.doGet(request, response);
	}
	
	//endXxx()方法是在ClientSide执行的,该方法对应testDoGet()
	public void endDoGet(WebResponse resp) throws SAXException {
		//这里使用com.meterware.httpunit.WebResponse,而非org.apache.cactus.WebResponse,前者提供了很多增强功能
		//但在使用httpunit中的WebResponse时,要额外借助Tidy.jar作为辅助包
		Assert.assertEquals(resp.getTables()[0].getCellAsText(0,0), "username");
		Assert.assertEquals(resp.getTables()[0].getCellAsText(0,1), "password");
	}
}


最后是JUnit3.8编写的测试套件TestAllUseJetty.java(为什么这么用,详见注释)

 

 

package com.jadyer.servlet;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.apache.cactus.extension.jetty.Jetty5xTestSetup;

/**
 * Jetty中使用Cactus测试Servlet步骤如下
 * @see 0)Cactus下载地址为http://archive.apache.org/dist/jakarta/cactus/
 * @see   关于Jetty的简单用法,可参考我的博文http://blog.csdn.net/jadyer/article/details/9299485
 * @see 1)加入所依赖的jar
 * @see   aspectjrt-1.5.3.jar
 * @see   cactus.core.framework.uberjar.javaEE.14-1.8.1.jar
 * @see   cactus.integration.ant-1.8.1.jar
 * @see   cactus.integration.shared.api-1.8.1.jar
 * @see   commons-codec-1.6.jar
 * @see   commons-discovery-0.4.jar
 * @see   commons-httpclient-3.1.jar
 * @see   commons-logging-1.1.jar
 * @see   geronimo-j2ee-management_1.0_spec-1.1.jar
 * @see   httpunit-1.6.jar
 * @see   nekoHTML.jar
 * @see   xercesMinimal.jar
 * @see   org.mortbay.jetty-5.1.9.jar(取自cactus-1.8.1-bin.zip)
 * @see 2)编写测试用例
 * @see   这里即LoginServletTest.java
 * @see 3)编写测试套件
 * @see   我们要在beginXxx之前启动Jetty,而Cactus不支持JUnit4中的annotation
 * @see   所以为了实现类似@BeforeClass的功能,我们就借助JUnit3.8中测试套件,最后测试时直接运行此测试套件即可
 * @see   关键JUnit3.8和JUnit4的测试套件编写规则,详见http://blog.csdn.net/jadyer/article/details/6013743
 * @create Jul 5, 2013 4:05:00 PM
 * @author 玄玉<http://blog.csdn.net/jadyer>
 */
public class TestAllUseJetty {
	public static Test suite(){
		//此时就不需要cactus.properties文件了,并且注意这里的值就不能以下划线结尾了,而端口则可任意设定
		//另外在web.xml中也不需要配置org.apache.cactus.server.ServletTestRedirector及其映射了
		System.setProperty("cactus.contextURL", "http://127.0.0.1:8088/testJettyAndCactus");
		
		TestSuite suite = new TestSuite();
		
		suite.addTestSuite(LoginServletTest.class);
		
		//记得要引入org.mortbay.jetty-5.1.9.jar,否则会报告java.lang.ClassNotFoundException: org.mortbay.jetty.Server
		return new Jetty5xTestSetup(suite);
	}
}
posted @ 2013-07-11 17:43  爱生活,爱编程  阅读(339)  评论(0编辑  收藏  举报