简体   繁体   English

Spring的MockMvc用于单元测试或集成测试吗?

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

Spring has 2 setups for the MockMvc: Spring有两个MockMvc设置:

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

In general what kind of testing is MockMvc used for? 一般来说,MockMvc用于什么样的测试? Unit or Integration? 单位还是整合? or Both? 或两者?

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设置可以编写集成测试?

Both forms are actually integration tests since you are testing the integration of your code with the Spring DispatcherServlet and supporting infrastructure. 这两种形式实际上都是集成测试,因为您正在测试代码与Spring DispatcherServlet和支持基础结构的集成。 The difference lies in the amount of supporting infrastructure that is used behind the scenes. 不同之处在于幕后使用的支持基础架构的数量。

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. “webAppContextSetup”加载实际的Spring MVC配置,从而产生更完整的集成测试。 Since the TestContext framework caches the loaded Spring configuration, it helps to keep tests running fast even as more tests get added. 由于TestContext框架缓存了加载的Spring配置,因此即使添加了更多测试,它也有助于保持测试快速运行。 Furthermore, you can inject mock services into controllers through Spring configuration, in order to remain focused on testing the web layer. 此外,您可以通过Spring配置将模拟服务注入控制器,以便专注于测试Web层。

... ...

The "standaloneSetup" on the other hand is a little closer to a unit test. 另一方面,“standaloneSetup”更接近单元测试。 It tests one controller at a time, the controller can be injected with mock dependencies manually, and it doesn't involve loading Spring configuration. 它一次测试一个控制器,控制器可以手动注入模拟依赖项,并且不涉及加载Spring配置。 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. 这些测试更侧重于样式,并且更容易查看正在测试哪个控制器,是否需要任何特定的Spring MVC配置,等等。 The "standaloneSetup" is also a very convenient way to write ad-hoc tests to verify some behavior or to debug an issue. “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. 使用“standaloneSetup”确实意味着需要一些额外的“webAppContextSetup”测试来验证Spring MVC配置。 Alternatively, you can decide to write all tests with "webAppContextSetup" and always test against actual Spring MVC configuration. 或者,您可以决定使用“webAppContextSetup”编写所有测试,并始终针对实际的Spring MVC配置进行测试。

... ...

The options provided in Spring MVC Test are different stops on the scale from classic unit to full integration tests. Spring MVC Test中提供的选项在从经典单元到完全集成测试的规模上是不同的停止。 To be sure none of the options in Spring MVC Test are classic unit tests but they are a little closer to it. 确保Spring MVC Test中没有一个选项是经典的单元测试,但它们更接近它。 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. 例如,您可以使用注入控制器的模拟隔离服务层,然后您只通过DispatcherServlet和实际的Spring配置测试Web层,就像您可以单独测试数据库层一样。 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. 如有疑问,我建议您在发布问题之前先阅读参考手册。 ;) ;)

Regards, 问候,

Sam ( author of the Spring TestContext Framework ) Sam( Spring TestContext Framework的作者

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 /people POST requests. WebApplicationContext设置正在加载整个上下文,因此您无需关心特定控制器的位置,例如/people POST请求。

So I would recommend using WebApplicationContext setup for testing your REST API in terms of interface which application need to work with. 所以我建议使用WebApplicationContext设置来根据应用程序需要使用的接口来测试REST API。 You dont couple test with actual code then + you are documenting the way the app should behave. 您不会将测试与实际代码相结合然后+您正在记录应用程序的行为方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM