`
pioneer127
  • 浏览: 5839 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
文章分类
社区版块
存档分类
最新评论

使用Cactus进行容器内测试三---(集成jetty进行容器内测试)

    博客分类:
  • tool
阅读更多

1、到http://dist.codehaus.org/jetty/下载jetty-5.x。

 

2、解压后在eclipse中配置jetty。

 

3、在WebTest项目中编写LoginServlet.java,其内容为:

package org.pioneer.test.servlet;

import java.io.IOException;

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

@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (isLogin(request)) {
			System.out.println("success");
		} else {
			System.out.println("failure");
		}
	}

	public boolean isLogin(HttpServletRequest request) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if ("hello".equals(username) && "world".equals(password)) {
			return true;
		}
		return false;
	}

}

 并在web.xml中配置LoginServlet。

 

4、然后编写LoginServlet.java的测试类LoginServletTest.java,其源代码如下:

package org.pioneer.test.servlet;

import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.apache.cactus.WebResponse;

public class LoginServletTest extends ServletTestCase {

	public void begin(WebRequest request) {
		System.out.println("begin session: " + session);
		System.out.println("begin");
	}

	public void end(WebResponse response) {
		System.out.println("end");
	}

	public void beginIsLogin(WebRequest request) {
		request.addParameter("username", "hello");
		request.addParameter("password", "world");
		System.out.println("beginIsLogin");
	}

	public void endIsLogin(WebResponse response) {
		System.out.println("endIsLogin");
	}

	@Override
	protected void setUp() throws Exception {
		System.out.println("setUp session: " + session);
		System.out.println("setUp");
	}

	@Override
	protected void tearDown() throws Exception {
		System.out.println("tearDown");
	}

	public void testIsLogin() {
		LoginServlet servlet = new LoginServlet();
		boolean result = servlet.isLogin(request);
		assertEquals(true, result);
	}

}

 

5、然后编写TestJettyAll.java,其源代码如下:

package org.pioneer.test.servlet;

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

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

public class TestJettyAll {

	public static Test suite() {
		System.setProperty("cactus.contextURL", "http://localhost:5555/hello");
		TestSuite suite = new TestSuite("Test All");
		suite.addTestSuite(LoginServletTest.class);
		return new Jetty5xTestSetup(suite);
	}
}

 直接运行TestJettyAll.java即可运行测试用例。

 

注:这个例子使用的是jetty的嵌入式启动方式。jetty也可以像tomcat一样运行。这个例子使用的是jetty-5.x版本。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics