Wednesday, 29 February 2012

How to mock http servlet with jetty to test some http-post,http-get in unit test?

Solution:
1. Write you own mock servlet
public class MockServlet extends HttpServlet {
    private String servletData;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        contextService(req, resp);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private void contextService(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
        servletData = IOUtils.toString(req.getInputStream());

        resp.setContentType("text/html");
        resp.setStatus(HttpServletResponse.SC_OK);
        resp.getWriter().print("Response from mock server: Submit is success.");
    }

    public String getServletData() {
        return servletData;
    }
}

2. define mock beans in spring application context (servlet and jetty server)
2.1 servlet:

<bean id="mockServlet" class="my.MockServlet"/>

2.2 jetty server

        
            
                
                    <property name="host" value="localhost"/>
                    <property name="port" value="8900"/>
                
            
        
        
            
                
                    <property name="contextPath" value="/"/>
                    
                        <bean class="org.mortbay.jetty.servlet.SessionHandler">
                    
                    <property name="resourceBase" value="c:/temp"/>
                    
                        
                            
                            
                                
                                    
                                    
                                        <property name="name" value="defaultServlet"/>
                                        
                                            <bean class="org.mortbay.jetty.servlet.DefaultServlet"/>
                                        
                                        
                                            
                                                <entry key="resourceBase" value="./"/>
                                            
                                        
                                    
                                    
                                        <property name="name" value="mockServlet"/>
                                        <property name="servlet" ref="mockServlet"/>
                                        
                                            
                                                <entry key="resourceBase" value="./"/>
                                            
                                        
                                    
                                
                            
                            
                                
                                    
                                        
                                            
                                                /
                                            
                                        
                                        <property name="servletName" value="defaultServlet"/>
                                    
                                    
                                        
                                            
                                                /test/myAction
                                            
                                        
                                        <property name="servletName" value="mockCharityServlet"/>
                                    
                                
                            
                        
                    
                
                
                
            
        
    


3. You Http Client code

final HttpPost post = new HttpPost("http://127.0.0.1:9800/test/myAction");
final HttpResponse resp = hc.execute(post);
final HttpEntity respContent = resp.getEntity();
final InputStream is = respContent.getContent();
final StringWriter strWr = new StringWriter();
IOUtils.copy(is, strWr);
return strWr.toString();

4. Junit
String expected = "Response from mock server: Submit is success.";
String actual = mockClient.submit();
assertEquals(expected, actual);

No comments:

Post a Comment