写一个servlet程序,再写一个junit测试类测试它
时间: 2023-06-16 11:04:32 浏览: 92
servlet练习程序
好的,下面是一个简单的servlet程序和对应的junit测试类:
```java
// MyServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h2>Hello, World!</h2>");
out.println("</body></html>");
}
}
```
```java
// MyServletTest.java
import org.junit.*;
import static org.junit.Assert.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServletTest {
private StringWriter stringWriter;
private PrintWriter writer;
private HttpServletRequest request;
private HttpServletResponse response;
private MyServlet servlet;
@Before
public void setUp() throws ServletException {
stringWriter = new StringWriter();
writer = new PrintWriter(stringWriter);
request = new MockHttpServletRequest();
response = new MockHttpServletResponse(writer);
servlet = new MyServlet();
servlet.init();
}
@Test
public void testDoGet() throws ServletException, IOException {
servlet.doGet(request, response);
String result = stringWriter.toString();
assertTrue(result.contains("Hello, World!"));
}
@After
public void tearDown() {
servlet.destroy();
}
private class MockHttpServletRequest implements HttpServletRequest {
public String getAuthType() { return null; }
public String getContextPath() { return null; }
public Cookie[] getCookies() { return null; }
public long getDateHeader(String name) { return 0; }
public String getHeader(String name) { return null; }
public Enumeration getHeaderNames() { return null; }
public Enumeration getHeaders(String name) { return null; }
public int getIntHeader(String name) { return 0; }
public String getMethod() { return null; }
public String getPathInfo() { return null; }
public String getPathTranslated() { return null; }
public String getQueryString() { return null; }
public String getRemoteUser() { return null; }
public String getRequestURI() { return null; }
public StringBuffer getRequestURL() { return null; }
public String getRequestedSessionId() { return null; }
public String getServletPath() { return null; }
public HttpSession getSession() { return null; }
public HttpSession getSession(boolean create) { return null; }
public Principal getUserPrincipal() { return null; }
public boolean isRequestedSessionIdFromCookie() { return false; }
public boolean isRequestedSessionIdFromURL() { return false; }
public boolean isRequestedSessionIdFromUrl() { return false; }
public boolean isRequestedSessionIdValid() { return false; }
public boolean isUserInRole(String role) { return false; }
public Object getAttribute(String name) { return null; }
public Enumeration getAttributeNames() { return null; }
public String getCharacterEncoding() { return null; }
public int getContentLength() { return 0; }
public String getContentType() { return null; }
public ServletInputStream getInputStream() { return null; }
public Locale getLocale() { return null; }
public Enumeration getLocales() { return null; }
public String getParameter(String name) { return null; }
public Map getParameterMap() { return null; }
public Enumeration getParameterNames() { return null; }
public String[] getParameterValues(String name) { return null; }
public String getProtocol() { return null; }
public BufferedReader getReader() { return null; }
public String getRealPath(String path) { return null; }
public String getRemoteAddr() { return null; }
public String getRemoteHost() { return null; }
public RequestDispatcher getRequestDispatcher(String path) { return null; }
public String getScheme() { return null; }
public String getServerName() { return null; }
public int getServerPort() { return 0; }
public boolean isSecure() { return false; }
public void removeAttribute(String name) { }
public void setAttribute(String name, Object value) { }
public void setCharacterEncoding(String encoding) { }
}
private class MockHttpServletResponse implements HttpServletResponse {
private PrintWriter writer;
public MockHttpServletResponse(PrintWriter writer) {
this.writer = writer;
}
public void addCookie(Cookie cookie) { }
public void addDateHeader(String name, long date) { }
public void addHeader(String name, String value) { }
public void addIntHeader(String name, int value) { }
public boolean containsHeader(String name) { return false; }
public String encodeRedirectURL(String url) { return null; }
public String encodeRedirectUrl(String url) { return null; }
public String encodeURL(String url) { return null; }
public String encodeUrl(String url) { return null; }
public void sendError(int sc) { }
public void sendError(int sc, String msg) { }
public void sendRedirect(String location) { }
public void setDateHeader(String name, long date) { }
public void setHeader(String name, String value) { }
public void setIntHeader(String name, int value) { }
public void setStatus(int sc) { }
public void setStatus(int sc, String sm) { }
public void flushBuffer() throws IOException { }
public int getBufferSize() { return 0; }
public String getCharacterEncoding() { return null; }
public String getContentType() { return null; }
public Locale getLocale() { return null; }
public ServletOutputStream getOutputStream() throws IOException { return null; }
public PrintWriter getWriter() throws IOException { return writer; }
public boolean isCommitted() { return false; }
public void reset() { }
public void resetBuffer() { }
public void setBufferSize(int size) { }
public void setContentLength(int len) { }
public void setContentType(String type) { }
public void setLocale(Locale loc) { }
}
}
```
上面的代码定义了一个简单的servlet,它在客户端请求时返回 "Hello, World!"。为了测试它,我们编写了一个junit测试类。在setUp()方法中,我们创建一个MyServlet实例并调用它的init()方法。在testDoGet()方法中,我们模拟一个HttpServletRequest和HttpServletResponse对象,并调用doGet()方法,然后检查响应是否包含 "Hello, World!"。在tearDown()方法中,我们调用MyServlet实例的destroy()方法以清理任何资源。MockHttpServletRequest和MockHttpServletResponse是模拟对象,以便我们可以在测试中使用这些对象而不依赖于Web容器。
阅读全文