1 cactus测试的原理
看下面这张图,这是一个测试工具Cactus测试Servlet的过程,而在过去测试Servlet是很麻烦的。 1.首先,JUnit测试器执行YYYTestCase.runTest(),这个函数找到beginXXX(WebRequest)执行它,这个过程发生在客户端。WebRequest作为参数,它包含了请求的HTTP头、HTTP参数,它将被传送到第2步的重定向代理器中。 2.YYYTestCase.runTest()建立一个到重定向代理器的HTTP链接. 上一步的WebRequest传了过去。 3.从客户端来看,重定向代理器好像是在服务器运行一样。这意味着你的测试用例类将被实例两次:一次是在客户端(被JUnit测试器实例),另一次是在服务器端(被重定向代理器实例)。客户端的实例用来执行beginXXX()和endXXX()函数,服务器端实例用来执行第4步中的testXXX()函数。 4.setUp(), testXXX()和tearDown()被依次执行,它们被重定向代理器以reflection机制执行。当然,setUp()和tearDown()是可选的(就像在JUnit里一样)。 5.你的testXXX()将调用服务器端代码测试,并使用JUnit的断言API来观察执行效果(如assert(), assertEquals(), fail(), ...)  6.如果测试失败,你的testXXX()函数抛出的异常将被重定向代理器捕获。 7.如果出现异常,重定向代理器返回它的信息(包含名字,类,栈顶数据)到客户端,然后异常信息将显示在JUnit的测试器上。 8.如果没有异常产生,YYYTestCase.runTest()函数则找到endXXX(org.apache.cactus.WebResponse)或endXXX(com.meterware.httpunit.WebResponse) 函数执行。在全过程中,你有机会在endXXX()函数检查HTTP头,Cookies以及servlet的输出流。
  
2.cactus 的安装
文件下载地址: 
http://www.javaresearch.org/oss/download/cactus/jakarta-cactus-12-1.6.1.zip 
使用方式: 
解压压缩包后将lib文件夹下的全部JAR添加到相关项目目录(为保证确认,可以将路径同时添加到classpath中),配置cactus.properties文件,修改cactus.contextURL值为所在webapp的起始路径,同时将该文件放置到classpath路径中 
如例: 
# Configuration file for Cactus. 
  
# Each project using Cactus need to have such a file put in the client side 
# CLASSPATH (Meaning the directory containgin this file should be in the client 
# side CLASSPATH, not the file itself of course ... :) ) 
  
# Defines the URLs that will be used by Cactus to call it's redirectors 
# (Servlet and JSP). You need to specify in these URLs the webapp context 
# that you use for your application. In the example below, the context is 
# "test". 
  
cactus.contextURL = http://localhost:8080/Fund_Cafe 
cactus.servletRedirectorName = ServletRedirector 
cactus.enableLogging=true 
修改WEB应用下web.xml文件添加相应的SERVLET映射 
如例; 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app> 
  <servlet> 
    <servlet-name>TS</servlet-name> 
    <servlet-class>jp.co.abic.wam.startmenu.ForwardLauncher</servlet-class> 
    <init-param> 
      <param-name>debug</param-name> 
      <param-value>2</param-value> 
    </init-param>     
    <init-param> 
      <param-name>WAM_HOME</param-name> 
      <param-value>D:\Fund_Cafe\conf</param-value> 
    </init-param> 
    <init-param> 
      <param-name>FORWARD_SERVLET</param-name> 
      <param-value>TS2</param-value> 
    </init-param> 
    <load-on-startup>2</load-on-startup>     
  </servlet> 
  <servlet> 
    <servlet-name>TS2</servlet-name> 
    <servlet-class>jp.co.abic.wam.WAMServlet</servlet-class> 
    <init-param> 
      <param-name>debug</param-name> 
      <param-value>2</param-value> 
    </init-param>   
    <init-param> 
      <param-name>WAM_HOME</param-name> 
      <param-value>D:\Fund_Cafe\conf</param-value> 
    </init-param> 
         <load-on-startup>2</load-on-startup>       
  </servlet>     
<servlet> 
  <servlet-name>ServletRedirector</servlet-name> 
  <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>ServletRedirector</servlet-name> 
    <url-pattern>/ServletRedirector</url-pattern> 
</servlet-mapping> 
  <servlet-mapping> 
    <servlet-name>TS</servlet-name> 
    <url-pattern>/TS</url-pattern> 
  </servlet-mapping> 
  <servlet-mapping> 
    <servlet-name>TS2</servlet-name> 
    <url-pattern>/TS2</url-pattern> 
  </servlet-mapping> 
  <resource-ref> 
        <description>Oracle DataSource example</description> 
        <res-ref-name>oraclePool</res-ref-name> 
        <res-type>javax.sql.DataSource</res-type> 
        <res-auth>Container</res-auth> 
  </resource-ref>   
</web-app> 
3.实际举例
被测试类: ForwardLauncher 
测试类: Test 
测试条件1:B=9914,S=123456789,ServerID=AA, BHD0001=1,E=0(程序正常路径,显示选择菜单) 
测试条件2:B=9914(程序异常路径,显示发生错误的页面) 
Test类内容如下: 
package jp.co.abic.wam; 
  
import java.io.IOException; 
  
import javax.servlet.ServletException; 
  
import jp.co.abic.wam.startmenu.ForwardLauncher; 
  
import org.apache.cactus.ServletTestCase; 
import org.apache.cactus.WebRequest; 
import org.apache.cactus.WebResponse; 
  
/** 
 * @author sfluo 
 * 
 * TODO To change the template for this generated type comment go to 
 * Window - Preferences - Java - Code Style - Code Templates 
 */ 
public class Test extends ServletTestCase { 
  
  
         ForwardLauncher wamServlet=new ForwardLauncher(); 
         public static void main(String[] args) { 
                   junit.textui.TestRunner.run(Test.class); 
         } 
  
         /* 
          * @see TestCase#setUp() 
          */ 
         protected void setUp() throws Exception { 
          
                   config.setInitParameter("FORWARD_SERVLET","TS2"); 
                   wamServlet.init(config); 
         } 
  
         /* 
          * @see TestCase#tearDown() 
          */ 
         protected void tearDown() throws Exception { 
                   super.tearDown(); 
         } 
  
         /* 
          * Class under test for void doGet(HttpServletRequest, HttpServletResponse) 
          */ 
         public void beginDoGetHttpServletRequestHttpServletResponseA(WebRequest theRequest){ 
                   theRequest.addParameter("B", "9914"); 
                   theRequest.addParameter("S", "123456789"); 
                   theRequest.addParameter("ServerID", "AA"); 
                   theRequest.addParameter("BHD0001", "1"); 
                   theRequest.addParameter("E", "0");    
         } 
         public final void testDoGetHttpServletRequestHttpServletResponseA() { 
                    
                   try { 
          
                            wamServlet.doGet(request,response); 
                   } catch (ServletException e) { 
                            // TODO Auto-generated catch block 
                            e.printStackTrace(); 
                   } catch (IOException e) { 
                            // TODO Auto-generated catch block 
                            e.printStackTrace(); 
                   } 
                    
                   //TODO Implement doGet(). 
         } 
         public final void endDoGetHttpServletRequestHttpServletResponseA(WebResponse theResponse){ 
                   System.out.print(theResponse.getText()); 
                   } 
  
         public void beginDoGetHttpServletRequestHttpServletResponseB(WebRequest theRequest){ 
                   theRequest.addParameter("B", "9914"); 
                    
         } 
         public final void testDoGetHttpServletRequestHttpServletResponseB() { 
                    
                   try { 
          
                            wamServlet.doGet(request,response); 
                   } catch (ServletException e) { 
                            // TODO Auto-generated catch block 
                            e.printStackTrace(); 
                   } catch (IOException e) { 
                            // TODO Auto-generated catch block 
                            e.printStackTrace(); 
                   } 
                    
                   //TODO Implement doGet(). 
         } 
         public final void endDoGetHttpServletRequestHttpServletResponseB(WebResponse theResponse){ 
                   System.out.print(theResponse.getText()); 
                    
                    
         } 
} 详细的api请参照相关的文档,在文件目录下均存在 
 
  |