Cactus借助Jetty测试Servlet (二)

2014-11-24 10:16:47 · 作者: · 浏览: 1
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");
}
}

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", "玄玉");
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(为什么这么用,详见注释)


[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) * @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不支持J