给servlet写单元测试的总结
<DIV>servlet的测试一般来说需要容器的支持,不是像通常的java类的junit测试一样简单,<BR> <BR>下面通过对HelloWorld代码的测试阐述了几种servlet测试方法。<BR> <BR>被测试的HelloWorld类的代码如下:<BR> <BR>/**<BR> * 被测试的servlet<BR> */</DIV><DIV>import java.io.IOException;<BR> <BR>import javax.servlet.http.HttpServlet;<BR>import javax.servlet.http.HttpServletRequest;<BR>import javax.servlet.http.HttpServletResponse;<BR>import org.apache.cactus.WebRequest; <BR>import org.apache.cactus.server.HttpServletRequestWrapper;<BR> <BR>public class HelloWorld extends HttpServlet{<BR> <BR> public void saveToSession(HttpServletRequest request) {</DIV>
<DIV> request.getSession().setAttribute("testAttribute",request.getParameter("testparam"));</DIV>
<DIV> }<BR> <BR> public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{</DIV>
<DIV> String username=request.getParameter("username");</DIV>
<DIV> response.getWriter().write(username+":Hello World!");<BR> <BR> }<BR> <BR> public boolean authenticate(){<BR> <BR> return true;<BR> <BR> }</DIV>
<DIV>}<BR> <BR>以HelloWorld为例,我总结了Servlet的多种测试方法如下:<BR> <BR>一.使用HttpUnit测试<BR> <BR>import com.meterware.httpunit.GetMethodWebRequest;<BR>import com.meterware.httpunit.WebRequest;<BR>import com.meterware.httpunit.WebResponse;<BR>import com.meterware.servletunit.InvocationContext;<BR>import com.meterware.servletunit.ServletRunner;<BR>import com.meterware.servletunit.ServletUnitClient;<BR>import junit.framework.Assert;<BR>import junit.framework.TestCase;<BR> <BR>public class HttpUnitTestHelloWorld extends TestCase {<BR> <BR> protected void setUp() throws Exception {<BR> super.setUp();<BR> }<BR> <BR> protected void tearDown() throws Exception {<BR> super.tearDown();<BR> }<BR> <BR> public void testHelloWorld() {<BR> <BR> try {</DIV>
<DIV> // 创建Servlet的运行环境</DIV>
<DIV> ServletRunner sr = new ServletRunner();</DIV>
<DIV> // 向环境中注册Servlet</DIV>
<DIV> sr.registerServlet("HelloWorld", HelloWorld.class.getName());<BR> <BR> // 创建访问Servlet的客户端</DIV>
<DIV> ServletUnitClient sc = sr.newClient();</DIV>
<DIV> // 发送请求</DIV>
<DIV> WebRequest request = new GetMethodWebRequest("<a href="http://localhost/HelloWorld" target="_blank">http://localhost/HelloWorld</A>");<BR> request.setParameter("username", "testuser");</DIV>
<DIV> InvocationContext ic = sc.newInvocation(request);</DIV>
<DIV> HelloWorld is = (HelloWorld) ic.getServlet();<BR> <BR> // 测试servlet的某个方法</DIV>
<DIV> Assert.assertTrue(is.authenticate());</DIV>
<DIV> // 获得模拟服务器的信息</DIV>
<DIV> WebResponse response = sc.getResponse(request);</DIV>
<DIV> // 断言</DIV>
<DIV> Assert.assertTrue(response.getText().equals("testuser:Hello World!"));</DIV>
<DIV> } catch (Exception e) {</DIV>
<DIV> e.printStackTrace();</DIV>
<DIV> }</DIV>
<DIV> }<BR> <BR>}<BR> <BR>上述例子其实是junit的一个测试例子,在其中使用了httpunit模拟的servlet环境,使用上述方法测试<BR> <BR>servlet可以脱离容器,容易把该测试写入ant或maven脚本,让测试进行。<BR> <BR>httpunit网址:<a href="http://httpunit.sourceforge.net/" target="_blank">http://httpunit.sourceforge.net/</A><BR> <BR>使用该种方法测试的弱点就是:如果要使用request(response)的setCharercterEncoding方法时,测试会出现一些问题,<BR> <BR>而且httpunit在测试servlet行为时,采用的是完全模拟浏览器,有时测试比较难写。<BR> <BR>二 使用cactus测试<BR> <BR>/**<BR> * cactus测试servlet的例子<BR> * 必须要有tomcat的支持<BR> * <BR> */<BR> <BR>import junit.framework.Test;<BR>import junit.framework.TestSuite;<BR>import org.apache.cactus.ServletTestCase; <BR>import org.apache.cactus.WebRequest;<BR>import org.apache.cactus.WebResponse;<BR>public class CactusHelloWorld extends ServletTestCase{<BR> <BR> HelloWorld servlet;<BR> public CactusHelloWorld(String theName) {<BR> super(theName);<BR> }<BR> <BR> protected void setUp() throws Exception {<BR> super.setUp();<BR> servlet = new HelloWorld();<BR> }<BR> <BR> protected void tearDown() throws Exception {<BR> super.tearDown();<BR> }<BR> <BR> /**<BR> * 测试方法测试参数在此设置<BR> * <BR> * @param webrequest<BR> */<BR> <BR> public void beginSaveToSessionOK(WebRequest request) {<BR> request.addParameter("testparam", "it works!");<BR> }<BR> <BR> /**<BR> * 测试方法测试参数在此设置<BR> * <BR> * @param webrequest<BR> */<BR> <BR> public void beginDoGet(WebRequest request) {<BR> request.addParameter("username", "testuser");<BR> }<BR> <BR> /**<BR> * 调用servlet的测试方法<BR> * <BR> */</DIV>
<DIV> public void testSaveToSessionOK() {<BR> servlet.saveToSession(request);<BR> assertEquals("it works!", session.getAttribute("testAttribute"));<BR> }<BR> <BR> public void testDoGet() {<BR> try {<BR> servlet.doGet(request, response);<BR> } catch (Exception e) {<BR> e.printStackTrace();<BR> }<BR> }<BR> <BR> /**<BR> * 此方法可以判断测试方法的输出,会传递测试方法的reponse给end***,并且格式化为cactus<BR> * 的WebResponse或者可以跟httpunit集成,格式化为httpunit的response<BR> * <BR> * @param response<BR> */</DIV>
<DIV> public void endDoGet(WebResponse response) {<BR> String content; <BR> content = response.getText();<BR> assertEquals("testuser:Hello World!", content);<BR> }<BR>}<BR> <BR>cactus具备丰富灵活的测试功能,如要测试doGet方法,分为beginDoGet(模拟测试参数设置)、DoGet(执行测试)、endDoGet(状态结果验证)<BR> <BR>相比httpunit来说,写测试更为容易,测试servlet更为专业,流程更为清晰,但是cactus需要容器支持,使得测试不可以自动进行,但是<BR> <BR>如果使用一个嵌入式的容器,测试就可以自动了。<BR> <BR>cactus是一个servlet和jsp的测试框架:http://jakarta.apache.org/cactus/getting_started.html<BR> <BR>三 使用Jetty作为嵌入式容器测试servlet.<BR> <BR>/**<BR> * 一个关于嵌入式jetty测试的例子,jetty作为stubs的一个例子<BR> * <BR> */<BR>package com.easyjf.testexample;<BR> <BR>import org.mortbay.jetty.Connector;<BR>import org.mortbay.jetty.Server;<BR>import org.mortbay.jetty.bio.SocketConnector;<BR>import org.mortbay.jetty.servlet.ServletHandler;<BR> <BR>import com.meterware.httpunit.WebClient;<BR>import com.meterware.httpunit.WebConversation;<BR>import com.meterware.httpunit.WebResponse;<BR> <BR>import junit.framework.Assert;<BR>import junit.framework.TestCase;<BR> <BR>public class JettySampleTest extends TestCase {<BR> <BR> Server server;<BR> protected void setUp() throws Exception {<BR> //通过代码设置并启动一个服务器,该服务器是servlet的测试容器<BR> super.setUp();<BR> server = new Server();<BR> Connector connector=new SocketConnector();<BR> connector.setPort(80);<BR> server.setConnectors(new Connector[]{connector});<BR> ServletHandler handler=new ServletHandler();<BR> server.setHandler(handler);<BR> handler.addServletWithMapping("HelloWorld", "/");<BR> server.start();<BR> }<BR> <BR> protected void tearDown() throws Exception {<BR> super.tearDown();<BR> server.stop();<BR> }<BR> <BR> public void testHellWorld() {<BR> try {<BR> WebConversation wc = new WebConversation();<BR> WebResponse web = wc.getResponse("<a href="http://127.0.0.1/HelloWorld" target="_blank">http://127.0.0.1/HelloWorld</A>");<BR> String result=web.getText();<BR> Assert.assertEquals(result,"it works!");<BR> } catch (Exception e) {<BR> e.printStackTrace();<BR> }<BR> }<BR>}<BR> <BR>可以发现,jetty可以充当一个servlet的容器,方便的是,jetty支持嵌入式服务,即可以通过代码来启动,<BR> <BR>所以要写自动测试的例子很方便,可以结合httpunit或者cactus进行servlet测试。<BR> <BR>jetty主页:<a href="http://docs.codehaus.org/display/JETTY/Embedding+Jetty" target="_blank">http://docs.codehaus.org/display/JETTY/Embedding+Jetty</A></DIV>
<DIV><BR>四 使用mock对象,此处使用easymock<BR> <BR>import java.io.PrintWriter;<BR>import java.io.Writer;<BR>import javax.servlet.ServletConfig;<BR>import javax.servlet.ServletContext;<BR>import javax.servlet.http.HttpServletRequest;<BR>import javax.servlet.http.HttpServletResponse;<BR> <BR>import junit.framework.Assert;<BR>import junit.framework.TestCase;<BR>import static org.easymock.EasyMock.*;<BR>public class MockTestServlet extends TestCase {<BR> <BR> public void testService() throws Exception {</DIV>
<DIV> System.out.println("service");</DIV>
<DIV> HttpServletRequest request = createMock(HttpServletRequest.class);</DIV>
<DIV> HttpServletResponse response = createMock(HttpServletResponse.class);</DIV>
<DIV> //Creating the ServletConfig mock here</DIV>
<DIV> ServletConfig servletConfig = createMock(ServletConfig.class);</DIV>
<DIV> //Creating the ServletContext mock here</DIV>
<DIV> ServletContext servletContext = createMock(ServletContext.class);<BR> <BR> //Create the target object <BR> <BR> HelloWorld4 instance = new HelloWorld();</DIV>
<DIV> //初始化servlet,一般由容器承担,一般调用servletConfig作为参数初始化,此处模拟容器行为</DIV>
<DIV> instance.init(servletConfig);<BR> <BR> //在某些方法被调用时设置期望的返回值,如下这样就不会去实际调用servletConfig的getServletContext方法,而是直接返回<BR> <BR> //servletContext,由于servletConfig是mock出来的,所以可以完全控制。</DIV>
<DIV> expect(servletConfig.getServletContext()).andReturn(servletContext).anyTimes();</DIV>
<DIV> expect(request.getParameter("username")).andReturn("testuser");</DIV>
<DIV> PrintWriter pw=new PrintWriter(System.out,true);</DIV>
<DIV> expect(response.getWriter()).andReturn(pw).anyTimes();<BR> <BR> //以上均是录制,下面为重放,该种机制为easymock测试机制,要理解请看easymock测试的一些资料<BR> replay(request);<BR> replay(response);<BR> replay(servletConfig);<BR> replay(servletContext);<BR> <BR> instance.doGet(request, response);</DIV>
<DIV> pw.flush();<BR> <BR> <BR> //验证结果是否预期,如果预期,则会在pw上写出testuser.<BR> verify(request);<BR> verify(response);<BR> verify(servletConfig);<BR> verify(servletContext);<BR> }<BR>}<BR> <BR>mock测试注重行为,mock对象其实都是模拟的对象,方法一般直接给出一个返回值,没有具体的对象逻辑,mock对象<BR> <BR>是用来帮助测试要测试的类的。比如要测试servlet的内部行为,又不想要容器等环境,就可以采用mock测试。<BR> <BR>easymock是mock测试的一个框架:<a href="http://www.easymock.org/" target="_blank">http://www.easymock.org/</A></DIV>
<DIV><BR>本文来自CSDN博客,转载请标明出处:<a href="http://blog.csdn.net/cz_hyf/archive/2007/02/10/1507211.aspx" target="_blank">http://blog.csdn.net/cz_hyf/archive/2007/02/10/1507211.aspx</A></DIV>
页:
[1]