java Spring 的 MockMvc 是用于单元测试还是集成测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32223490/
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-11-02 19:51:06  来源:igfitidea点击:

Are Spring's MockMvc used for unit testing or integration testing?

javaspringspring-mvcspring-testspring-test-mvc

提问by SoftwareDeveloper

Spring has 2 setups for the MockMvc:

Spring 有 2 个 MockMvc 设置:

  1. Standalone setup
  2. WebApplicationContext setup
  1. 独立设置
  2. WebApplicationContext 设置

In general what kind of testing is MockMvc used for? Unit or Integration? or Both?

MockMvc一般用于什么样的测试?单元还是集成?或两者?

Am i right in saying that using the standalone setup (running outside the Spring's application context) allows you to write unit tests and with the WebApplicationContext setup you can write integration tests?

我说使用独立设置(在 Spring 的应用程序上下文之外运行)允许您编写单元测试并且使用 WebApplicationContext 设置您可以编写集成测试是否正确?

回答by Sam Brannen

Both forms are actually integration testssince you are testing the integration of your code with the Spring DispatcherServletand supporting infrastructure. The difference lies in the amount of supporting infrastructure that is used behind the scenes.

这两种形式实际上都是集成测试,因为您正在测试代码与 SpringDispatcherServlet和支持基础设施的集成。不同之处在于幕后使用的支持基础设施的数量。

The details are documented in the Spring reference manual.

详细信息记录在 Spring 参考手册中。

Noteworthy excerpts:

值得注意的摘录:

The "webAppContextSetup" loads the actual Spring MVC configuration resulting in a more complete integration test. Since the TestContext framework caches the loaded Spring configuration, it helps to keep tests running fast even as more tests get added. Furthermore, you can inject mock services into controllers through Spring configuration, in order to remain focused on testing the web layer.

“webAppContextSetup”加载实际的 Spring MVC 配置,从而产生更完整的集成测试。由于 TestContext 框架缓存加载的 Spring 配置,因此即使添加了更多测试,它也有助于保持测试快速运行。此外,您可以通过 Spring 配置将模拟服务注入控制器,以便专注于测试 web 层。

...

...

The "standaloneSetup" on the other hand is a little closer to a unit test. It tests one controller at a time, the controller can be injected with mock dependencies manually, and it doesn't involve loading Spring configuration. Such tests are more focused in style and make it easier to see which controller is being tested, whether any specific Spring MVC configuration is required to work, and so on. The "standaloneSetup" is also a very convenient way to write ad-hoc tests to verify some behavior or to debug an issue.

另一方面,“standaloneSetup”更接近于单元测试。它一次测试一个控制器,控制器可以手动注入模拟依赖项,并且不涉及加载 Spring 配置。这样的测试在风格上更集中,并且更容易查看正在测试的控制器,是否需要任何特定的 Spring MVC 配置,等等。“standaloneSetup”也是编写临时测试以验证某些行为或调试问题的一种非常方便的方法。

...

...

Just like with integration vs unit testing, there is no right or wrong answer. Using the "standaloneSetup" does imply the need for some additional "webAppContextSetup" tests to verify the Spring MVC configuration. Alternatively, you can decide to write all tests with "webAppContextSetup" and always test against actual Spring MVC configuration.

就像集成与单元测试一样,没有正确或错误的答案。使用“standaloneSetup”确实意味着需要一些额外的“webAppContextSetup”测试来验证 Spring MVC 配置。或者,您可以决定使用“webAppContextSetup”编写所有测试,并始终针对实际的 Spring MVC 配置进行测试。

...

...

The options provided in Spring MVC Test are different stops on the scale from classic unit to full integration tests. To be sure none of the options in Spring MVC Test are classic unit tests but they are a little closer to it. For example you can isolate the service layer with mocks injected into controllers and then you're testing the web layer only through the DispatcherServlet and with actual Spring configuration, just like you might test the database layer in isolation of the layers above. Or you could be using the standalone setup focusing on one controller at a time and manually providing the configuration required to make it work.

Spring MVC 测试中提供的选项在从经典单元到完整集成测试的规模上是不同的。可以肯定的是,Spring MVC Test 中的所有选项都不是经典的单元测试,但它们更接近于它。例如,您可以使用注入到控制器中的模拟来隔离服务层,然后您仅通过 DispatcherServlet 和实际的 Spring 配置测试 Web 层,就像您可以在隔离上述层的情况下测试数据库层一样。或者,您可以使用一次专注于一个控制器的独立设置,并手动提供使其工作所需的配置。

When in doubt, I suggest first reading the reference manual before posting questions here. ;)

如有疑问,我建议先阅读参考手册,然后再在此处发布问题。;)

Regards,

问候,

Sam (author of the Spring TestContext Framework)

Sam(Spring TestContext Framework 的作者

回答by freakman

I would say that both methods are for integration testing, but standalone force you to specify which controller you are testing.

我会说这两种方法都用于集成测试,但是独立的方法强制您指定要测试的控制器。

WebApplicationContext setup is loading whole context, so you don't care where is specific controller which serves for example /peoplePOST requests.

WebApplicationContext 设置正在加载整个上下文,因此您不必关心为例如/peoplePOST 请求提供服务的特定控制器在哪里。

So I would recommend using WebApplicationContext setup for testing your REST API in terms of interface which application need to work with. You dont couple test with actual code then + you are documenting the way the app should behave.

因此,我建议使用 WebApplicationContext 设置来根据应用程序需要使用的接口来测试您的 REST API。您没有将测试与实际代码结合起来,然后 + 您正在记录应用程序的行为方式。