如何使用 JUnit 测试 Servlet?
我看到 Jetty 有一个 ServletTest 类,你可以用它来测试 Servlet。
tester = new ServletTester();
tester.setContextPath("/");
tester.addServlet(TestServlet.class, "/servlet/*");
...
tester.start();
不知道Tomcat中有没有类似的东西? 我如何测试 servlet?
I saw Jetty has got and ServletTest class which you can use to test Servlets.
tester = new ServletTester();
tester.setContextPath("/");
tester.addServlet(TestServlet.class, "/servlet/*");
...
tester.start();
Do you know if there is something similar in Tomcat?
How could I test a servlet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HttpUnit 有一个名为 ServletUnit 的“模拟容器”。
HttpUnit has a has a "simulated container" called ServletUnit.
看看雅加达仙人掌
这是 servlet 测试方法
Take a look at Jakarta Cactus
Here's a servlet test how-to
我从未发现直接测试 servlet(或者说 Struts 操作)有什么好处,特别是考虑到这样做所需的工作。
我的大多数 servlet/actions/无论什么都使用 POJO 来完成大部分工作,并且 POJO 经过严格测试。 Web 应用程序本身具有 HtmlUnit 测试套件。我认为两者之间的一切都只是管道。
我不相信我曾经遇到过任何类型的错误,这些错误只能通过直接测试 servlet 类来捕获,而 POJO 或 webapp 测试不会捕获这些错误。
I've never found a benefit to testing servlets directly (nor Struts actions, say), especially given the work needed to do it.
Most of my servlets/actions/whatever use POJOs for the bulk of their work, and the POJOs are heavily tested. The webapps themselves have suites of HtmlUnit tests. Everything in between I assume to be just plumbing.
I don't believe that I've even once encountered any sort of bug that would have ONLY been caught by testing the servlet classes directly, and which would not be caught by the POJO or webapp tests.