views:

381

answers:

1

I'm trying to run Jettys ServletTester in my JUnit test. I created a simple HelloServlet first to test the setup, but I get an IllegalAccessException when I try to request the servlet. Here is what I have so far:

My unit test

@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());
}

My HelloServlet

This servlet is defined in the same file as the unit test, because I want it to be there for the initial setup of jetty. After everything is running, I'll remove it (or maybe keep it, but it will stay within the unit test then).

Update This servlet was defined inside the unit test itself because it was meant only as a configuration test for the jetty server itself. But jetty wasn't able to access it, and after moving it into a public class and a file for itself everything worked like expected. See the comment.

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();
    }
}

My Exception...

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 [email protected]: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)

Strange enough, because I got this example almost straight from http://docs.codehaus.org/display/JETTY/ServletTester. Any thoughts or maybe a working example of a embedded jetty servlet container in a junit test?

+2  A: 

Your HelloServlet must be implemented as a public class:

public class HelloServlet extends HttpServlet { ... }
jarnbjo
Sorry, I forgot to mention that my <code>HelloServlet</code> is defined in the same file as the unit test.
cringe
Oh, crap. It has to be defined outside the test to be public so that the ServletHolder can access it... thanks a lot. :-)
cringe