I want to write a junit test to verify a tomcat 7 behavior, which is related to the Servlet 3.0 annotations (@WebServlet
, @ServletSecurity
).
What I have so far is this test:
@Test
public void testDoGet()
throws ServletException, LifecycleException, IOException {
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
Context ctx = tomcat.addWebapp("/",
new File("target").getAbsolutePath());
Tomcat.addServlet(ctx, "hello", HelloServlet.class.getName());
ctx.addServletMapping("/hello", "hello");
tomcat.start();
ByteChunk chunk = new ByteChunk();
int responceCode = getUrl("http://127.0.0.1:8080/hello",chunk);
System.out.println(responceCode + ": " + chunk.toString());
tomcat.stop();
}
This test works, but it does not pay any respect to the annotation.
It is clear that this test does not pay any respect to the annotations at HelloServlet. But I have no clue how to “register” a servlet so that its annotation based configuration is used. Can anyone give me a hint how to build a test that do that?