模拟集成测试 [英] Mocking for integration tests

查看:66
本文介绍了模拟集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何模拟集成测试所需的许多依赖关系?

How does one mock the many dependencies needed for integration tests?

我使用Mockito进行纯"单元测试.在这种情况下,纯"意味着测试单个类,模拟其所有依赖项.美丽.

I use Mockito for my 'pure' unit tests. 'Pure' in this case means testing a single class, mocking all of it's dependencies. Beautiful.

现在进行集成测试.假设在这种情况下,集成测试将测试如下内容:

Now come integration tests. Let's say in this case an integration test will test something like this:

  1. 消息放入队列
  2. 消息已处理"
  3. 响应消息被放入响应队列

我们也可以说步骤2中发生的处理是很严重的事情.它依赖于许多数据库交互,多个外部服务,文件系统以及各种事物.流还会触发很多副作用,因此我不能简单地确保响应正确-我需要验证副作用.

Let's also say that the processing that happens in step 2 is serious stuff. It relies on lots of database interactions, on multiple external services, the file system, all kinds of things. There are also a lot of side effects that the flow will trigger, so I cannot simply ensure that the response is correct - I need to verify the side effects.

每个依赖项都由单个无状态服务类包装,这使它们变得美观且可模拟.

Each of these dependencies are wrapped by a single stateless service class, which makes them nice and mockable.

人们如何处理?

我很想使用Mockito,以便可以验证上述流程将产生的副作用.但是,Mocktio的文档(在很大程度上是其实现)似乎在纯"单元测试以外的环境中强烈反对使用它.我已经尝试过这条路线,但是

I would love to use Mockito so that I could verify the side effects that the above flow will have. However, Mocktio's documentation (and to a large extent it's implementation) seems to fight strongly against using it in contexts other than 'pure' unit tests. I've tried to go this route, but

  • 填充存根数据非常困难(因为数据很多)
  • Spring很难将那些存根实例插入我的bean中
  • 很难重置"模拟,以便在不清除存根的情况下验证一组不同的交互.

编辑

我知道我可以使用HSQLDB实例之类的东西来处理数据库问题,但是仍然存在外部服务问题.为了实现可重复性,我不能依赖那些服务正常运行,处于所需状态等等.我看到的唯一选择就是模拟它们.

I know that I could handle the database issue with something like an HSQLDB instance, but there's still the issue of the external services. For repeatability I can't rely on those services being up, being in the state that I require, etc. The only option I see there is to mock them.

Whatdaya做什么?

Whatdaya do?

推荐答案

好问题.

似乎您达到了Mockito的极限.如果您想检查对象之间的交互,则Mockito很棒.

It seems like you hit the limits of Mockito. Mockito is great if what you want to inspect object interactions.

但是,您想要的似乎是更高抽象级别的可观察性(和可控制性).恐怕您需要的模拟或存根应该经过精心设计和手工制作.

What you want, though, seems to be observability (and controllability) at a higher level of abstraction. I'm afraid that the mocks or stubs you need for that should be carefully designed and hand-crafted.

在单元级别,可以通过Mockito很好地生成这些模拟.在集成级别,这变得更加困难,并且您将需要专用的可测试性接口.

At the unit level, these mocks can be nicely generated, by means of Mockito. At the integration level, this becomes much harder, and you will need purpose made testability interfaces.

这篇关于模拟集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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