一個cactus測試的例子,用eclipse

來源:互聯網
上載者:User
以前apache提供cactus的eclipse外掛程式,但是因為cactus是基於juint的,所以現在沒有外掛程式了。
另外apache上面的文檔關於如何整合到eclipse的部分還沒有換。
http://jakarta.apache.org/cactus/integration/eclipse/runner_plugin.html和http://jakarta.apache.org/cactus/integration/eclipse/webapp_plugin.html上面提到的 org.apache.cactus.integration.eclipse_[version]現在在apache上是找不到的。

http://jakarta.apache.org/cactus/integration/manual/howto_config.html給出了應該如何配置的問題。

另,上面的快速入門未看過:
http://jakarta.apache.org/cactus/integration/howto_tomcat.html

下面是轉貼的:http://blog.csdn.net/eagle51998/archive/2006/07/10/899550.aspx
注意:如果你只是測試servlet,那麼在web.xml中只需要有servlet的相關配置即可。
並且,按照
http://jakarta.apache.org/cactus/integration/manual/howto_config.html,cactus.properties中除了cactus.contextURL 屬性,其他都是可選的。

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;
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.