一个cactus测试的例子,用eclipse

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

你可能感兴趣的:(eclipse)