Cactus 用于服务器端测试框架,是对Junit的一个扩展,但它与Junit有一些不同,Cactus的测试分为三种:JspTestCase,ServletTestCase,FilterTestCase.
做一个ServletTestCase例子:
应用到的架包:
aspectjrt-1.2.1.jar
cactus-1.7.2.jar
cactus-ant-1.7.2.jar
cargo-0.5.jar commons-httpclient-2.0.2.jar
commons-logging-1.0.4.jar
geronimo-spec-j2ee-1.4-rc4.jar
httpunit-1.6.jar
jasper-compiler-4.1.30.jar
jasper-runtime-4.1.30.jar
junit-3.8.1.jar
nekohtml-0.9.1.jar
org.mortbay.jetty-4.2.17.jar
servletapi-2.3.jar
所需的文件:
web.xml:
<description>
Cactus Demo
</description>
<display-name>Cactus Demo</display-name>
<servlet>
<servlet-name>ServletRedirector</servlet-name>
<servlet-class>
org.apache.cactus.server.ServletTestRedirector
</servlet-class>
</servlet>
<servlet>
<servlet-name>ServletTestRunner</servlet-name>
<servlet-class>
org.apache.cactus.server.runner.ServletTestRunner
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletRedirector</servlet-name>
<url-pattern>/ServletRedirector</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletTestRunner</servlet-name>
<url-pattern>/ServletTestRunner</url-pattern>
</servlet-mapping>
cactus.properties
cactus.jspRedirectorName = JspRedirector
cactus.contextURL = http://localhost:8090/cactus/
cactus.servletRedirectorName = ServletRedirector
cactus.filterRedirectorName = FilterRedirector
cactus.enableLogging=true
Servlet类:
package com.servlet;
import javax.servlet.http.HttpServletRequest;
public class ServletSample {
public boolean isEqual(HttpServletRequest request) {
String name1 = request.getParameter("name1");
String name2 = request.getParameter("name2");
if(name1.equals(name2)) {
return true;
}
else {
return false;
}
}
}
TestCase类:
public class TestServlet extends ServletTestCase{
public void begin(WebRequest webRequest) throws ParserConfigurationException, SAXException, IOException {
String name1 = "test";
String name2 = "test";
webRequest.addParameter("name1",name1);
webRequest.addParameter("name2",name2 );
}
public void test() {
ServletSample servletSample = new ServletSample();
assertTrue(servletSample.isEqual(request));
}
}
TestServlet继承的是ServletTestCase
如何跑测试类:
可以通过 ANT 和 URL访问进行测试。
以URL为例:
http://localhost:8090/cactus/ServletTestRunner?suite=com.test.TestServlet
cactus-report.xsl这个文件是设置测试结果所显示的样式,可以通过下面URL进行查看。
http://localhost:8090/cactus/ServletTestRunner?suite=com.test.TestServlet&xsl=cactus-report.xsl