在JUnit测试中运行Jettys ServletTester

时间:2009-10-20 07:40:13

标签: unit-testing servlets junit jetty

我正在尝试在JUnit测试中运行Jettys ServletTester。我首先创建了一个简单的HelloServlet来测试设置,但是当我尝试请求servlet时,我得到一个IllegalAccessException。以下是我到目前为止的情况:

我的单元测试

@Before
public void setUp() throws Exception {
    tester = new ServletTester();
    tester.setContextPath("/context");
    tester.addServlet(HelloServlet.class, "/hello/*");
    tester.start();
}

@After
public void tearDown() throws Exception {
    tester.stop();
}

@Test
public void testDefaultServlet() throws Exception {
    HttpTester request = new HttpTester();
    request.setMethod("GET");
    request.setHeader("Host","127.0.0.1");
    request.setURI("/context/hello/info");
    request.setVersion("HTTP/1.0");

    HttpTester response = new HttpTester();
    response.parse(tester.getResponses(request.generate()));

    assertNull(response.getMethod());
    assertEquals(200,response.getStatus());
    assertEquals("<h1>Hello Servlet</h1>",response.getContent());
}

我的HelloServlet

这个servlet与单元测试在同一个文件中定义,因为我希望它可以用于jetty的初始设置。一切运行后,我会删除它(或者保留它,但它会保留在单元测试中)。

更新这个servlet是在单元测试中定义的,因为它只是作为jetty服务器本身的配置测试。但是jetty无法访问它,并且在将它移动到公共类和文件后,一切都按预期工作。请参阅评论。

class HelloServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        out.println("Hello, World!");
        out.flush();
    }
}

我的例外......

2009-10-20 09:36:28.973::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
 2009-10-20 09:36:28.989::INFO:  jetty-6.1.21
 2009-10-20 09:36:29.098::INFO:  Started LocalConnector@0.0.0.0:1
 2009-10-20 09:36:29.161:/context:WARN:  unavailable
 java.lang.IllegalAccessException: Class org.mortbay.jetty.servlet.Holder can not access a member of class my.package.HelloServlet with modifiers ""
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
    at java.lang.Class.newInstance0(Class.java:349)
    at java.lang.Class.newInstance(Class.java:308)
    at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
    at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:428)
    at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:536)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:915)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:539)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:405)
    at org.mortbay.jetty.LocalConnector.accept(LocalConnector.java:186)
    at org.mortbay.jetty.AbstractConnector$Acceptor.run(AbstractConnector.java:707)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

很奇怪,因为我从http://docs.codehaus.org/display/JETTY/ServletTester几乎直接得到了这个例子。在junit测试中嵌入式jetty servlet容器的任何想法或可能的实例

1 个答案:

答案 0 :(得分:7)

您的HelloServlet必须作为公共类实现:

public class HelloServlet extends HttpServlet { ... }