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

标签 java spring spring-mvc spring-test spring-test-mvc

Spring 有两个 MockMvc 设置:

  1. 独立设置
  2. WebApplicationContext 设置

一般来说,MockMvc 用于什么样的测试?单元还是集成?还是两者都有?

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

最佳答案

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

详细信息记录在 Spring 引用手册中。

值得注意的摘录:

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.

...

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.

...

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.

...

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.

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

问候,

Sam(Spring TestContext 框架的作者)

关于java - Spring 的 MockMvc 是用于单元测试还是集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32223490/

相关文章:

java - 使用 hibernate-envers 时,AUD 表中的所有 @Version 字段均为 null,但在实体中 - 表已填充好吗?

spring - Jedis连接异常: Could not get a resource from the pool

java - 将独立的 Spring 应用程序转换或集成到 Spring MVC 应用程序

java - Spring DateTime Conversion服务异常

java - 给定指向该节点的指针,删除链表的最后一个节点

java - 无法从字符串值 ('{' 实例化 java.util.LinkedHashMap 类型的值;没有单字符串构造函数/工厂方法

java - 用于将参数传递给方法的 Eclipse 键盘快捷键

java - 从其他 fragment 回到上一个 fragment

spring - @Autowired(required=false) 在构造函数上给出 NoSuchBeanDefinitionException

java - Spring 5 样板项目无法运行