`
ray_yui
  • 浏览: 216970 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Cactus使用

阅读更多

前言:本文章需要JUnit单元测试框架的基础知识,若读者还不具备,请阅读笔者的JUnit文章:http://ray-yui.iteye.com/blog/1914106



UnitTest系列文章:
      使用JUnit开发单元测试:http://ray-yui.iteye.com/blog/1914106
      使用DBUnit扩展JUnit:http://ray-yui.iteye.com/blog/1914979
      使用EasyMock扩展Junithttp://ray-yui.iteye.com/blog/1916170
      使用Spring TestContext测试Spring应用http://ray-yui.iteye.com/blog/1921424
      使用Cobertura生成测试覆盖率报告http://ray-yui.iteye.com/blog/1921958



什么是Cactus?
      Cactus是Apache下的一个开源测试Web层的框架,可以完成模拟J2EE的容器来进行测试,可以测试Servlet,JSP,Filter,EJB等等,以下图片为Cactus官方网站的原理图
                                   

为什么要使用Cactus?
      当测试DAO时我们可以使用DBUnit来进行对数据库的隔离,当我们测试Service的时候可以使用EasyMock来模拟DAO的实现进行Service和DAO的隔离,虽然我们测试Servlet的时候也可以使用EasyMock来对Session,Request等来进行模拟,但实际上我们会发现,使用Mock来模拟Servlet只能测试简单的Servlet,例如Session,Request,Parameter等,对一些Servlet的返回值,返回了什么View等,测试起来还是力不从心,使用Cactus可以产生模拟的容器来对Servlet进行测试,大大减低了测试难度和提高了测试效率

Cactus使用:
      增加Maven依赖

<dependency>
	<groupId>httpunit</groupId>
	<artifactId>httpunit</artifactId>
	<version>1.7</version>
</dependency>
<dependency>
	<groupId>org.apache.cactus</groupId>
	<artifactId>cactus.core.framework.uberjar.javaEE.15</artifactId>
	<version>1.8.1</version>
</dependency>


      在maven管理下的src/test/resources/中增加cactus.properties

cactus.contextURL=http://localhost:8080/test


      以下为被测试的Servlet

package com.accentrix.ray.test.unit;

import java.io.IOException;

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

import com.accentrix.ray.entity.User;

public class LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6916692964267082204L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String userName = req.getParameter("username");
		if (userName == null || "".equals(userName.trim()))
			resp.sendRedirect("/error.jsp");
		else
			req.getRequestDispatcher("/success.jsp").forward(req, resp);
	}
}


      以下为测试类

package com.accentrix.ray.test.unit;

import java.io.IOException;

import javax.servlet.ServletException;

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

import com.meterware.httpunit.WebResponse;

public class TestLoginServlet extends ServletTestCase {

	private LoginServlet loginServlet;

	/*
	 * 由begin开始的方法,都被视为客户端方法,这里的操作 都应该是从
	 * 客户端发出的,例如客户端传递给服务端的参数就应该从begin方法
	 * 中传入,参数WebRequest为生成request而需要
	 */
	public void beginLogin(WebRequest request) {
		// 通过request.addParameter()来添加parameter
		request.addParameter("username", "Ray");
	}

	/*
	 * 初始化对象
	 */
	public void setUp() {
		loginServlet = new LoginServlet();
	}

	/*
	 * 测试doGet方法
	 */
	public void testDoGet() throws ServletException, IOException {
		// 注意,此时request和response为ServletTestCase的属性
		loginServlet.doGet(super.request, super.response);
	}

	/*
	 * 由end开始的方法,都被视为服务器响应方法,这里操作
	 * 的应该是验证服务器传回来的结果,例如返回的JSP是否正确
	 * ,参数WebResponse为验证提供了大量的方法,例如getTable
	 */
	public void endDoGet(WebResponse response) throws SAXException {
		//返回已编译后的html页面的title
		assertEquals("test", response.getTitle());
		//返回整个html的document
		assertEquals("test", response.getDOM().getElementById("Ray").getNodeValue());
		//返回指定名称的元素
		assertEquals(3, response.getElementsByTagName("input").length);
	}
}


      编写完毕后,还需要把项目布置到服务器当中,开启服务器后运行test case

注意事项:
      1.在cactus.properties文件中,contextURL的值需要注意后面不能添加 '/',
        例如http://localhost:8080/test/ <--这样是错误的,因为Cactus会寻找
        http://localhost:8080/test/ServletRedirector来解释测试类

      2.Cactus1.8.1版本目前还不支持Junit4的Annotation,所以开发这需要遵循
        Junit3.8的规范来开发,例如@Before需要用setUp()来替换

      3.编写test case时,beginXXX testXXX endXXX为一个组合,xxx为自定义名字,
        不同组合的begin和end不会互相影响

      4.在上述例子中endDoGet(WebResponse response)中WebResponse中的类型
        为httpunit中的实现类com.meterware.httpunit.WebResponse,若开发者
        使用的是Cactus中的org.apache.cactus.WebRequest时,将不能使用getTable(),
        getTitle等方法

      Cactus是功能非常强大的Web容器测试框架,除了测试Servlet还可以测试JSP,Filter,EJB等,当有实际需求时,可以上Cactus官方网站进行学习,上面都有非常详细的Demo和解释http://jakarta.apache.org/cactus/



总结:
      笔者在学习Cactus时发现可以使用Jetty嵌入式服务器达到在测试前启动服务器的效果,但学习过后Demo并不能成功运行,所以请有兴趣的读者参考其他资料,笔者觉得在实际开发当中没有使用Cactus的必要,因为Web层是离前台最近的地方,通过界面点击进行参数等的观察都非常方便,测试的价值并没有Service和DAO的大,而且Cactus虽然功能强大,但易用性并不是太好.建议读者遇到实际需求时认真选择是否使用Cactus
  • 大小: 17.2 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics