【发布时间】:2011-03-14 12:34:46
【问题描述】:
我想编写一个 junit 测试来验证与 Servlet 3.0 注释(@WebServlet、@ServletSecurity)相关的 tomcat 7 行为。
到目前为止我所做的是这个测试:
@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();
}
这个测试有效,但它不尊重注释。
很明显,这个测试没有考虑到 HelloServlet 的注解。 但我不知道如何“注册”一个 servlet 以便使用其基于注释的配置。谁能给我一个提示如何构建一个可以做到这一点的测试?
【问题讨论】:
-
tomcat-7.0.8->7.0.11 中存在一个错误,其中@servletSecurity 被忽略。也许您的测试是正确的并且您无事可做,您只是触发了错误并且您的测试失败了。 mail-archive.com/users@tomcat.apache.org/msg87337.html
-
@Jean:我知道这个错误,但我使用的是 7.0.11。
标签: java unit-testing tomcat servlets