eclipse 单元测试 servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:09:08  来源:igfitidea点击:

Unit-testing servlets

eclipseunit-testingtomcatservletsjunit

提问by Yuval F

I have a bunch of servlets running under the Tomcat servlet container. I would like to separate test code from production code, so I considered using a test framework. JUnit is nicely integrated into Eclipse, but I failed to make it run servlets using a running Tomcat server. Could you please recommend a unit testing framework that supports testing Tomcat servlets? Eclipse integration is nice but not necessary.

我有一堆 servlet 在 Tomcat servlet 容器下运行。我想将测试代码与生产代码分开,所以我考虑使用测试框架。JUnit 很好地集成到 Eclipse 中,但我未能使用正在运行的 Tomcat 服务器使其运行 servlet。你能推荐一个支持测试Tomcat servlet的单元测试框架吗?Eclipse 集成很好,但不是必需的。

采纳答案by Scott Bale

Check out ServletUnit, which is part of HttpUnit. In a nutshell, ServletUnit provides a library of mocks and utilities you can use in ordinary JUnit tests to mock out a servlet container and other servlet-related objects like request and response objects. The link above contains examples.

查看ServletUnit,它是 HttpUnit 的一部分。简而言之,ServletUnit 提供了一个模拟和实用程序库,您可以在普通 JUnit 测试中使用它来模拟 servlet 容器和其他与 servlet 相关的对象,如请求和响应对象。上面的链接包含示例。

回答by WMR

The Spring Framework has nice ready made mock objects for several classes out of the Servlet API:

Spring 框架为 Servlet API 中的几个类提供了很好的现成模拟对象:

http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/mock/web/package-summary.html

http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/mock/web/package-summary.html

回答by Will Sargent

Okay. Ignoring the 'tomcat' bit and coding to the servlet, your best bet is to create mocks for the response and request objects, and then tell it what you expect out of it.

好的。忽略 'tomcat' 位并对 servlet 进行编码,最好的办法是为响应和请求对象创建模拟,然后告诉它您对它的期望。

So for a standard empty doPost, and using EasyMock, you'll have

因此,对于标准的空 doPost,并使用EasyMock,您将拥有

public void testPost() {
   mockRequest = createMock(HttpServletRequest.class);
   mockResponse = createMock(HttpServletResponse.class);
   replay(mockRequest, mockResponse);
   myServlet.doPost(mockRequest, mockResponse);
   verify(mockRequest, mockResponse);
}

Then start adding code to the doPost. The mocks will fail because they have no expectations, and then you can set up the expectations from there.

然后开始向 doPost 添加代码。模拟将失败,因为它们没有期望,然后您可以从那里设置期望。

Note that if you want to use EasyMock with classes, you'll have to use the EasyMock class extension library. But it'll work the same way from then on.

请注意,如果您想将 EasyMock 与类一起使用,则必须使用 EasyMock 类扩展库。但从那时起它将以相同的方式工作。

回答by Raedwald

Separate the parts of that code that deal with HTTP requests and response from the parts that do business logic or data-base manipulation. In most cases this will produce a three tier architecture, with a data-layer (for the data-base/persistence), service-layer (for the business logic) and a presentation-layer (for the HTTP requests and responses).

将处理 HTTP 请求和响应的代码部分与执行业务逻辑或数据库操作的部分分开。在大多数情况下,这将产生一个三层架构,具有数据层(用于数据库/持久性)、服务层(用于业务逻辑)和表示层(用于 HTTP 请求和响应)。

  1. You can unit test the first two layers without any servlet stuff at all; it will be easier to test that way.
  2. You can test the presentation layer, as others suggest, using mock HTTP request and response objects.
  3. Finally, if you feel it really is necessary, you can do integration tests using a too such as HtmlUnitor JWebUnit.
  1. 您可以在没有任何 servlet 内容的情况下对前两层进行单元测试;这样测试会更容易。
  2. 您可以像其他人建议的那样,使用模拟 HTTP 请求和响应对象来测试表示层。
  3. 最后,如果您觉得确实有必要,您可以使用诸如HtmlUnitJWebUnit 之类的工具进行集成测试。

回答by Argelbargel

For "in-container" testing, have a look at Cactus

对于“容器内”测试,请查看Cactus

If you want to be able to test without a running container you can either simulate its components with your own mockobjects (e.g. with EasyMock) or you could try MockRunnerwhich has "pre-defined" Stubs for testing servlets, jdbc-connections etc.

如果您希望能够在没有运行容器的情况下进行测试,您可以使用您自己的模拟对象(例如使用EasyMock)模拟其组件,或者您可以尝试MockRunner,它具有用于测试 servlet、jdbc 连接等的“预定义”存根。

回答by Mike Kaufman

Updated Feb 2018: OpenBrace Limited has closed down, and its ObMimic product is no longer supported.

2018 年 2 月更新:OpenBrace Limited 已关闭,不再支持其 ObMimic 产品。

If you want a newer alternative to ServletUnit for JUnit testing of Servlets, you might find my company's ObMimiclibrary useful. It's available for free from the website's downloadspage.

如果您想要 ServletUnit 的更新替代品来对 Servlet 进行 JUnit 测试,您可能会发现我公司的ObMimic库很有用。它可以从网站的下载页面免费获得。

As with ServletUnit, it provides a library of classes that you can use in normal JUnit or TestNG tests outside of any servlet container to simulate the Servlet API.

与 ServletUnit 一样,它提供了一个类库,您可以在任何 servlet 容器之外的普通 JUnit 或 TestNG 测试中使用这些类来模拟 Servlet API。

Its Servlet API objects have no-argument constructors, are fully configurable and inspectable for all relevant Servlet API data and settings, and provide a complete simulation of all of the behaviour specified by the Servlet API's javadoc. To help with testing there's support for selective recording of Servlet API calls, control over any container-dependent behaviour, checks for any ambiguous calls (i.e. where the Servlet API behavour isn't fully defined), and an in-memory JNDI simulation for any servlet code that relies on JNDI lookups.

它的 Servlet API 对象具有无参数构造函数,对于所有相关的 Servlet API 数据和设置是完全可配置和可检查的,并提供对 Servlet API 的 javadoc 指定的所有行为的完整模拟。为了帮助测试,支持选择性记录 Servlet API 调用、控制任何依赖于容器的行为、检查任何不明确的调用(即 Servlet API 行为未完全定义的地方)以及内存中的 JNDI 模拟任何依赖于 JNDI 查找的 servlet 代码。

For full details, example code, "how to" guides, Javadoc etc, see the website.

有关完整的详细信息、示例代码、“操作方法”指南、Javadoc 等,请参阅网站。