我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

Cactus容器测试入门

此文参考: http://jakarta.apache.org/cactus/integration/howto_tomcat.html

我们在web.xml中的webapp之间加入:
<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>ServletTestRunner</servlet-name>
  
<servlet-class>org.apache.cactus.server.runner.ServletTestRunner</servlet-class>
</servlet>
加入mapping:
<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>


然后我们写一个简单的Servlet来进行测试:
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

public class SampleServlet extends HttpServlet
{
    
public void saveToSession(HttpServletRequest request)
    
{
        String testparam 
= request.getParameter("testparam");
        request.getSession().setAttribute(
"testAttribute", testparam);
    }

}


对于此Servlet在xml的配置我就不写了.想必你也了然于心了吧.

来看看我们的测试类:
import junit.framework.Test;
import junit.framework.TestSuite;

import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;

public class TestSampleServlet extends ServletTestCase
{
    
public TestSampleServlet(String theName)
    
{
        
super(theName);
    }


    
public static Test suite()
    
{
        
return new TestSuite(TestSampleServlet.class);
    }


    
public void beginSaveToSessionOK(WebRequest webRequest)
    
{
        webRequest.addParameter(
"testparam""it works!");
    }


    
public void testSaveToSessionOK()
    
{
        SampleServlet servlet 
= new SampleServlet();
        servlet.saveToSession(request);
        assertEquals(
"it works!", session.getAttribute("testAttribute"));
    }

}



部署之后,启动Tomcat,访问:

http://localhost:8080/test/ServletTestRunner?suite=TestSampleServlet
这时候你会看到输出 :

<?xml version="1.0" encoding="UTF-8" ?> 
- <testsuites>
- <testsuite name="com.test.servlet.TestServletTest" tests="1" failures="0" errors="1" time="0.516">
- <testcase name="testSaveSessionToOK" time="0.36">
  
<error message="org/apache/commons/codec/DecoderException" type="java.lang.NoClassDefFoundError">java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException at org.apache.commons.httpclient.HttpMethodBase.getURI(HttpMethodBase.java:265) at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:383) at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:346) at org.apache.cactus.internal.client.connector.http.HttpClientConnectionHelper.connect_aroundBody0(HttpClientConnectionHelper.java:144) at org.apache.cactus.internal.client.connector.http.HttpClientConnectionHelper.connect_aroundBody1$advice(HttpClientConnectionHelper.java:307) at org.apache.cactus.internal.client.connector.http.HttpClientConnectionHelper.connect(HttpClientConnectionHelper.java:1) at org.apache.cactus.internal.client.connector.http.DefaultHttpClient.callRunTest(DefaultHttpClient.java:163) at org.apache.cactus.internal.client.connector.http.DefaultHttpClient.doTest_aroundBody0(DefaultHttpClient.java:81) at org.apache.cactus.internal.client.connector.http.DefaultHttpClient.doTest_aroundBody1$advice(DefaultHttpClient.java:307) at org.apache.cactus.internal.client.connector.http.DefaultHttpClient.doTest(DefaultHttpClient.java:1) at org.apache.cactus.internal.client.connector.http.HttpProtocolHandler.runWebTest(HttpProtocolHandler.java:164) at org.apache.cactus.internal.client.connector.http.HttpProtocolHandler.runTest_aroundBody0(HttpProtocolHandler.java:83) at org.apache.cactus.internal.client.connector.http.HttpProtocolHandler.runTest_aroundBody1$advice(HttpProtocolHandler.java:307) at org.apache.cactus.internal.client.connector.http.HttpProtocolHandler.runTest(HttpProtocolHandler.java:1) at org.apache.cactus.internal.client.ClientTestCaseCaller.runTest(ClientTestCaseCaller.java:145) at org.apache.cactus.internal.AbstractCactusTestCase.runBareClient(AbstractCactusTestCase.java:218) at org.apache.cactus.internal.AbstractCactusTestCase.runBare(AbstractCactusTestCase.java:134) at org.apache.cactus.server.runner.ServletTestRunner.run(ServletTestRunner.java:309) at org.apache.cactus.server.runner.ServletTestRunner.doGet_aroundBody0(ServletTestRunner.java:187) at org.apache.cactus.server.runner.ServletTestRunner.doGet_aroundBody1$advice(ServletTestRunner.java:225) at org.apache.cactus.server.runner.ServletTestRunner.doGet(ServletTestRunner.java:1) at javax.servlet.http.HttpServlet.service(HttpServlet.java:690) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Unknown Source)</error> 
  
</testcase>
  
</testsuite>
  
</testsuites>

看来程序没有大问题,已经正确执行.不过这时提示有一个类找不到.
对于这个问题我们可以去http://commons.apache.org/downloads/download_codec.cgi下载.

这个xml看起来多少不习惯.我们可以下载xsl把它转换成html格式.
点此下载 把它放到你web应用的根目录下.

这个时候你可以访问如下地址:
http://localhost:8080/test/ServletTestRunner?suite=TestSampleServlet&xsl=cactus-report.xsl

这时候你就会看到友好的Html格式了:
HTML output of ServletTestRunner


posted on 2008-06-01 01:11 々上善若水々 阅读(3238) 评论(1)  编辑  收藏 所属分类: 软件测试

评论

# re: Cactus容器测试入门  回复  更多评论   

Hello....

Thanks a lot.... it helped me to run my first cactus program
2012-11-12 19:01 | Cyril

只有注册用户登录后才能发表评论。


网站导航: