• 使用jmock对HttpServlet类进行单元测试


    1.创建UploadServlet类

    package com.anllin.servlet;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.output.Format;
    import static com.anllin.servlet.XmlUtils.*;

    public class UploadServlet extends HttpServlet
    {
    private static final long serialVersionUID = 7320350794641788612L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
    String ipAndPort = String.format("http://%s:%s", req.getServerName(), req.getLocalPort());
    String serviceProviderXml = constructResponseXMl(ipAndPort);
    resp.setCharacterEncoding("utf-8");
    resp.setContentType("text/xml");
    PrintWriter out = resp.getWriter();
    out.append(serviceProviderXml);
    out.flush();
    out.close();
    }

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

    protected String constructResponseXMl(String ipAndPort)
    {
    Document doc = ConstructDocument();
    Element root = constructElement("ServiceProvider");
    doc.addContent(root);

    Element title = constructElement("title", "Test");
    Element description = constructElement("description", "Upload data in everyday");
    Element url = constructElement("url", ipAndPort + "/test/upload");

    constructChildTree(root, title, description, url);

    Format format = getFormat("utf-8", " ");
    String xml = getXMLStringFromDocunment(doc, format);
    return xml;
    }
    }

    2.创建辅助类XmlUtils

    package com.anllin.servlet;

    import java.io.StringReader;
    import java.util.Arrays;
    import java.util.List;

    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;

    public class XmlUtils
    {
    public static Element parse(String xml)
    {
    try
    {
    SAXBuilder builder = new SAXBuilder();
    StringReader sr = new StringReader(xml);
    Document doc = builder.build(sr);
    return doc.getRootElement();
    }
    catch (Exception e)
    {
    throw new RuntimeException(e);
    }
    }

    public static Document ConstructDocument()
    {
    return new Document();
    }

    public static Element constructElement(String name)
    {
    return new Element(name);
    }

    public static Element constructElement(String name, String text)
    {
    return new Element(name).setText(text);
    }

    public static Element constructChildTree(Element father, Element... childs)
    {
    List<Element> list = Arrays.asList(childs);
    father.addContent(list);
    return father;
    }

    public static Format getFormat(String encoding, String indent)
    {
    return Format.getPrettyFormat().setEncoding(encoding).setIndent(indent);
    }

    public static XMLOutputter getXMLOutputter(Format format)
    {
    return new XMLOutputter(format);
    }

    public static String getXMLStringFromDocunment(Document doc, Format format)
    {
    return getXMLOutputter(format).outputString(doc);
    }
    }

    3.创建MockedTests类

    package com.anllin.servlet;

    import org.jmock.Mockery;
    import org.jmock.integration.junit4.JMock;
    import org.jmock.integration.junit4.JUnit4Mockery;
    import org.junit.runner.RunWith;

    @RunWith(JMock.class)
    public class MockedTests
    {
    protected Mockery mockery = new JUnit4Mockery();
    }

    4.使用jmock进行测试

    package com.anllin.servlet;

    import static org.junit.Assert.*;

    import java.io.ByteArrayOutputStream;
    import java.io.PrintWriter;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.jdom.Element;
    import org.jmock.Expectations;
    import org.junit.Before;
    import org.junit.Test;

    public class UploadServletTest extends MockedTests
    {
    private UploadServlet uploadServlet;
    private HttpServletRequest req;
    private HttpServletResponse resp;
    private ByteArrayOutputStream out;

    @Before
    public void setUp() throws Exception
    {
    uploadServlet = new UploadServlet();
    req = mockery.mock(HttpServletRequest.class);
    resp = mockery.mock(HttpServletResponse.class);
    out = new ByteArrayOutputStream();
    }

    @Test
    public void testDoGet() throws Exception
    {
    mockery.checking(new Expectations()
    {
    {
    one(req).getServerName();
    will(returnValue("192.168.0.62"));

    one(req).getLocalPort();
    will(returnValue(8080));

    one(resp).setCharacterEncoding("utf-8");
    one(resp).setContentType("text/xml");

    PrintWriter writer = new PrintWriter(out);
    allowing(resp).getWriter();
    will(returnValue(writer));
    }
    });

    uploadServlet.doGet(req, resp);
    Element root = XmlUtils.parse(out.toString());
    assertTrue(root.getChildren().size() > 0);
    }
    }

    测试结果输出为:

    <?xml version="1.0" encoding="utf-8"?>
    <ServiceProvider>
    <title>Test</title>
    <description>Upload data in everyday</description>
    <url>http://192.168.0.62:8080/test/upload</url>
    </ServiceProvider>





     

  • 相关阅读:
    JDK各版本新特性总结
    Java MyBatis3(3)分页插件PageHelper
    Kubernetes--K8S
    MySQL 查看表结构
    查看mysql库大小,表大小,索引大小
    Linux服务器安装Oracle服务端总结
    怎样玩转千万级别的数据
    Markdown 新手指南
    深入理解Java:注解(Annotation)基本概念
    深入理解Java:注解(Annotation)自定义注解入门
  • 原文地址:https://www.cnblogs.com/zfc2201/p/2308131.html
Copyright © 2020-2023  润新知