Spring的MockMvc是用于单元测试还是集成测试? [英] Are Spring's MockMvc used for unit testing or integration testing?

查看:172
本文介绍了Spring的MockMvc是用于单元测试还是集成测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring有两个MockMvc设置:

Spring has 2 setups for the MockMvc:


  1. 独立设置

  2. WebApplicationContext设置

一般来说,MockMvc用于什么样的测试?单位还是整合?或两者?

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

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

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 DispatcherServlet 和支持基础结构。不同之处在于幕后使用的支持基础架构数量。

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

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

The details are documented in the Spring reference manual.

  • Server-Side Tests
  • Setup Options
  • Difference With End-to-End Integration Tests

值得注意的摘录:


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

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.

...


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

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确实意味着需要一些
额外的webAppContextSetup测试来验证Spring MVC
配置。或者,您可以决定使用
webAppContextSetup编写所有测试,并始终针对实际的Spring MVC
配置进行测试。

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.

...


Spring MVC Test中提供的选项是
等级从经典单位到完整单位的不同止损集成测试。为了确保没有
Spring MVC Test中的选项是经典的单元测试,但是它们距离它更接近
。例如,您可以将服务层
与注入控制器的模拟隔离,然后您只通过DispatcherServlet和实际的Spring
配置测试web
层,就像您可能测试
中的数据库层隔离上面的层。或者您可以使用独立的
设置,一次关注一个控制器,并手动提供使其工作所需的
配置。

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.

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

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

问候,

Sam( Spring TestContext Framework的作者

这篇关于Spring的MockMvc是用于单元测试还是集成测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆