怎样将eclipse与Cactus集成,做Container内的单元测试?

本文介绍了如何将Cactus框架与Eclipse集成,以进行Container内的单元测试。首先,列举了所需的资源和类库,如Eclipse SDK、Cactus及相关依赖。接着,详细说明了在`cactus.properties`配置文件中的设置,并指导将其放置于项目`WEB-INF/classes`目录下。然后,在`web.xml`中添加Cactus的Servlet和Filter配置,以便测试请求通过Web Container过滤。最后,展示了一个测试类的编写和运行步骤,包括如何使用JUnit运行测试套件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cactus是apache的一个开源测试框架,它是建立在junit 的基础上,它的功能比junit 强大的多,它可以做服务器端的测试,特别是Container 内的测试,它是一个自动测试框架,经过一段时间的研究,将我的心得写下来供大家参考。

首先需要的资源如下:

1 . Eclipse SDK Version: 3.1.2

2.  需要的类库有:aspectjrt-1.2.1.jar,  cactus-1.7.1.jar ,commons-httpclient-2.0.2.jar,commons-digester.jar,httpunit-1.6.jar, junit.jar。

3. 将服务器与eclipse集成(可选,集成进来的主要目的是可以实现debug)

下面是需要编写的文件资源:

1。cactus.properties

################################################################################

# Configuration file for Cactus.

# Each project using Cactus need to have such a file put in the CLASSPATH
# (Meaning the directory containgin this file should be in the 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.servletRedirectorURL = http://localhost:7001/cactus/ServletRedirector
cactus.jspRedirectorURL = http://localhost:7001/cactus/JspRedirector
cactus.filterRedirectorURL = http://localhost:7001/cactus/FilterRedirector


# Name of Cactus property that specify the URL up to the webapp context.
# This is the base URL to call for the redirectors. It is made up of :
# "http://" + serverName + port + "/" + contextName.
# 修改cactus.contextURL值为所在webapp的起始路径

cactus.contextURL = http://localhost:7001/cactus

cactus.servletRedirectorName = ServletRedirector
cactus.jspRedirectorName = JspRedirector
cactus.filterRedirectorName = FilterRedirector


# Name of the Cactus property for defining an initializer (i.e. a class
# that is executed before the Cactus tests start on the client side).

cactus.initializer=

cactus.enableLogging=true
################################################################################

将cactus.properties放在project 的WEB-INF下的Classes 目录下,这样的话,在Weblogic启动的时候就可以直接找到这个配置文件。

2.  在web.xml中添加cactus的测试类,(类似于路由器)发送的请求,经过Web Container进行过滤

web.xml

================================================================================

<!-- config for cactus  -->
   <context-param>
      <param-name>param</param-name>
      <param-value>value used for testing</param-value>
    </context-param>
   
    <servlet>
        <servlet-name>ServletRedirector</servlet-name>
        <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
        <init-param>
          <param-name>param1</param-name>
          <param-value>value1 used for testing</param-value>
        </init-param>
    </servlet>
   
    <servlet>
        <servlet-name>ServletRedirector_TestOverride</servlet-name>
        <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
        <init-param>
          <param-name>param2</param-name>
          <param-value>value2 used for testing</param-value>
        </init-param>
    </servlet>

    <servlet>
        <servlet-name>TestJsp</servlet-name>
        <jsp-file>/test/test.jsp</jsp-file>
    </servlet>

    <servlet>
        <servlet-name>JspRedirector</servlet-name>
        <jsp-file>/test/jspRedirector.jsp</jsp-file>
        <init-param>
          <param-name>param1</param-name>
          <param-value>value1 used for testing</param-value>
        </init-param>
    </servlet>
   
    <servlet-mapping> 
         <servlet-name>ServletRedirector</servlet-name> 
         <url-pattern>/ServletRedirector</url-pattern> 
   </servlet-mapping>
 
 <servlet>
      <servlet-name>ServletTestRunner</servlet-name>
      <display-name>ServletTestRunner</display-name>      
      <servlet-class>org.apache.cactus.server.runner.ServletTestRunner</servlet-class>
 </servlet>

<servlet-mapping>
      <servlet-name>ServletTestRedirector</servlet-name>
      <url-pattern>/ServletRedirector</url-pattern>
</servlet-mapping>

<servlet-mapping>
      <servlet-name>ServletTestRunner</servlet-name>
      <url-pattern>/ServletTestRunner</url-pattern>
</servlet-mapping>
 
 
 <servlet-mapping> 
    <servlet-name>JspRedirector</servlet-name> 
      <url-pattern>/JspRedirector</url-pattern> 
 </servlet-mapping>
 
 <servlet-mapping>
        <servlet-name>ServletRedirector_TestOverride</servlet-name>
        <url-pattern>/ServletRedirectorOverride</url-pattern>
  </servlet-mapping>
    
  <filter>
       <filter-name>FilterRedirector</filter-name>
       <filter-class>org.apache.cactus.server.FilterTestRedirector</filter-class>
  </filter>
 
  <filter-mapping>
     <filter-name>FilterRedirector</filter-name>
     <url-pattern>/FilterRedirector</url-pattern>
 </filter-mapping>

================================================================================

3. 编写一个测试类

public class TestDelegate extends ServletTestCase
{
 private TestDelegate delegate = new TestDelegate (); 
 
  public void setUp() throws Exception
  {

      //   begin some initial work

      //  construct some Context

  }
 
  public void testAdd() throws BusinessException
  {  

      Dto dto = new Dto();

      delegate .add(dto);

      delegate.update(dto);

      delegate.delete(dto);

  }

}

运行步骤:

启动Weblogic ,确保在Web Container下可以找到ServletRedirector等Servlet

使用Junit 来运行程序,还可以使用Test Suit来执行测试套件

================================================================================

public class TestAll
{
 public static Test suite()
    {
        TestSuite suite = new TestSuite(
            "Cactus unit tests for J2EE 1.3");

        // Add shared tests
        //suite.addTest(TestShareAll.suite());

        // Test cases specific to J2EE 1.3 only
        suite.addTestSuite(TestHello.class);
        suite.addTestSuite(TestWorld.class);
        suite.addTestSuite(TestDelegate.class);
        suite.addTestSuite(TestEJB.class);
        suite.addTestSuite(TestOther.class);

        return suite;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值