模拟框架对我有什么作用? [英] What does a mocking framework do for me?

查看:84
本文介绍了模拟框架对我有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说有些我无法交谈的人是jmock的忠实拥护者.我已经完成了以测试为中心的开发工作多年,因此我浏览了网站并查看了一些文档,但仍然不知道它有什么好处.

我在春天遇到了同样的问题.如果您已经了解它的含义,那么他们的文档就可以很好地解释它,因此我不认为jmock没有任何价值.我只是不明白它对我有什么作用.

因此,如果jmock为我提供了模拟存根数据的功能,那么让我们来看一个如何做事的示例,看看jmock会变得更好.

比方说,我有一个UI层,它表示创建一个窗口小部件和窗口小部件服务,当创建窗口小部件时,初始化窗口小部件并将其片段存储在组成窗口小部件所需的三个表中.

编写测试时,这是我的工作方式.

首先,我将休眠状态重新指向我的测试超音速数据库,因此不必进行大量数据库设置. Hibernate为我创建了我的表.

我对类的所有测试都具有静态工厂方法,这些方法为我构造了该类的测试实例.我的每个DAO都会创建指向测试架构的测试版本.然后,我的服务类将使用测试类生成的DAO构造自己.

现在,当我运行对调用该服务的UI控制器的测试时,我将在整个应用程序中测试我的代码.可以肯定的是,这并不是进行单元测试时通常需要的完全隔离,但是在我看来,它为我提供了更好的单元测试,因为它可以在所有支持层中一直执行真实的代码.

由于Hypersonic处于休眠状态的速度很慢,因此运行我的所有测试花费的时间稍长一些,但是对于完整的构建和打包,我的整个构建仍在一台旧计算机上运行不到五分钟,因此,我认为这是可以接受的. /p>

我如何用jmock做些不同的事情?

解决方案

在您的示例中,有两个接口可以使用模拟框架进行适当的单元测试:

  • UI层和窗口小部件服务之间的接口-用模拟代替窗口小部件服务将使您能够单独测试UI层,该服务返回手动创建的数据,并且模拟可验证预期的服务调用(和没有其他人.
  • 窗口小部件服务和DAO之间的接口-通过用模拟替换DAO,可以单独测试任何包含复杂逻辑的服务方法.

保证这不是进行单元测试时通常需要的完全隔离,但是在我看来,它为我提供了更好的单元测试,因为它可以在所有支持层中一直执行真实的代码.

这似乎是您问题的核心.答案有很多方面:

  • 如果您不是孤立地测试组件,则没有单元测试,而有集成测试.如您所见,这些都是非常有价值的,但是它们也有缺点
  • 由于它们同时测试更多的事物,因此它们倾向于更频繁地中断,它们倾向于以较大的组中断(当通用功能存在问题时),并且当它们这样做时,很难找出实际的位置问题所在
  • 在可以测试哪种方案方面,他们受到更大的限制.在集成测试中很难或不可能模拟某些边缘情况.
  • 有时候,由于某些组件在您的控制之下(例如第三方Web服务)不足以设置所需的测试数据,因此无法自动执行完整的集成测试.在这种情况下,您甚至可能会在高级集成测试中使用模拟框架.

I have heard some people who I cannot talk to are big fans of jmock. I've done test centered development for years and so I went through the website and looked at some of the docs and still can't figure out what good it is.

I had the same problem with spring. Their docs do a great job explaining it if you already understand what it is, so I'm not assuming that jmock is of no value. I just don't understand what it does for me.

So if jmock provides me with the ability to mock out stubbed data, let's go with an example of how I do things and see how jmock would be better.

Let's say I have my UI layer that says, create me a widget and the widget service, when creating a widget, initializes the widget and stores pieces of it in the three tables necessary to make up a widget.

When I write my tests, here's how I go about it.

First, I re-point hibernate to my test hypersonic database so I don't have to do a bunch of database set up. Hibernate creates my tables for me.

All of my tests for my classes have static factory methods that construct a test instance of the class for me. Each of my DAOs create test versions that point to the test schema. Then my service class has one that constructs itself with DAOs generated by the test class.

Now, when I run my test of the UI controller that calls the service, I am testing my code all the way through the application. Granted that this is not the total isolation generally wanted when doing a unit test, but it provides me, in my opinion, a better unit test because it executes the real code all the way through all of the supporting layers.

Because Hypersonic under hibernate is slow, it takes slightly longer to run all of my tests, but my entire build still runs in less than five minutes on an older computer for full build and packaging, so I find that pretty acceptable.

How would I do things differently with jmock?

解决方案

In your example, there are two interfaces where one would use a mocking framework to do proper unit tests:

  • The interface between the UI layer and the widget service - replacing the widget service with a mock would allow you to test the UI layer in isolation, with the service returning manually created data and the mock verifying that the expected service calls (and no others) happen.
  • The interface between the widget service and the DAO - by replacing the DAO with a mock, any service methods that contain complex logic can be tested in isolation.

Granted that this is not the total isolation generally wanted when doing a unit test, but it provides me, in my opinion, a better unit test because it executes the real code all the way through all of the supporting layers.

This seems to be the core of your question. The answer has a number of facets:

  • If you're not testing components in isolation, you do not have unit tests, you have integration tests. As you observe, these are quite valuable, but they have their drawbacks
  • Since they test more things at the same time, they tend to break more often, they tend to break in large groups (when there's a problem with common functionality) and when they do, it is harder to find out where the actual problem lies
  • They are more constrained in what kinds of scenarios you can test. It can be hard or impossible to simulate certain edge cases in an integration test.
  • Sometimes a full integration test cannot be automated because some component is not sufficiently under your control (e.g. a third-party webservice) to set up the test data you need. In such a case you might even end up using a mocking framework in what is otherwise a high-level integration test.

这篇关于模拟框架对我有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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