使用Cactus进行容器内测试
1、到http://jakarta.apache.org/cactus/downloads.html下载Cactus所需要的jar包及Cactus的依赖的jar包(commons-codec-1.3.zip,其下载地址为:http://commons.apache.org/downloads/download_codec.cgi)
2、创建一个名为WebTest的web项目,将步骤1中下载的jar包加入到项目的classpath中,配置web.xml文件:添加如下两个servlet配置。
<servlet> <servlet-name>ServletTestRunner</servlet-name> <servlet-class>org.apache.cactus.server.runner.ServletTestRunner</servlet-class> </servlet> <servlet> <servlet-name>ServletRedirector</servlet-name> <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletTestRunner</servlet-name> <url-pattern>/ServletTestRunner</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ServletRedirector</servlet-name> <url-pattern>/ServletRedirector</url-pattern> </servlet-mapping>
3、编写一个servlet,FirstServlet.java的源代码如下:
package org.pioneer.test.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (isLogin(request)) { System.out.println("success"); } else { System.out.println("failure"); request.getSession().setAttribute("user", "user"); } } public boolean isLogin(HttpServletRequest request) { if (null == request.getSession().getAttribute("user")) { return false; } return true; } }
并在web.xml中添加FirstServlet的配置:
<servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>org.pioneer.test.servlet.FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping>
4、编写FirstServlet.java对应的测试类FirstServletTest.java,源代码如下所示:
package org.pioneer.test.servlet; import org.apache.cactus.ServletTestCase; public class FirstServletTest extends ServletTestCase { public void testIsLogin() { FirstServlet servlet = new FirstServlet(); boolean result = servlet.isLogin(request); assertEquals(false, result); } }
5、编写TestAll.java类用于运行测试用例,其源代码如下所示:
package org.pioneer.test.servlet; import junit.framework.Test; import junit.framework.TestSuite; public class TestAll { public static Test suite() { TestSuite suite = new TestSuite("Test All"); suite.addTestSuite(FirstServletTest.class); return suite; } }
6、部署项目并打开浏览器访问http://localhost:8080/WebTest/ServletTestRunner?suite=org.pioneer.test.servlet.TestAll将会看到测试结果
<?xml version="1.0" encoding="UTF-8" ?> <testsuites> <testsuite name="org.pioneer.test.servlet.TestAll" tests="1" failures="0" errors="0" time="0.688"> <testcase name="testIsLogin" time="0.578" /> </testsuite> </testsuites>
7、到http://jakarta.apache.org/cactus/integration/integration_browser.html下载cactus-report.xsl并将其放在WebRoot下,再在浏览器中请求http://localhost:8080/WebTest/ServletTestRunner?suite=org.pioneer.test.servlet.TestAll&xsl=cactus-report.xsl将看到html格式的测试结果。