何时使用Mockito.verify()? [英] When to use Mockito.verify()?

查看:1699
本文介绍了何时使用Mockito.verify()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为了3个目的编写了jUnit测试用例:

I write jUnit test cases for 3 purposes:


  1. 为了确保我的代码满足所有必需的功能,大多数输入组合/值。

  2. 确保我可以更改实现,并依赖JUnit测试用例告诉我仍然满足所有功能。

  3. 作为我的代码处理的所有用例的文档,并作为重构规范 - 如果代码需要重写。 (重构代码,如果我的jUnit测试失败 - 你可能错过了一些用例)。

我不明白为什么或何时应使用 Mockito.verify()。当我看到 verify()被调用时,它告诉我我的jUnit正在意识到实现。 (因此,即使我的功能未受影响,更改我的实现也会破坏我的jUnits)。

I do not understand why or when Mockito.verify() should be used. When I see verify() being called, it is telling me that my jUnit is becoming aware of the implementation. (Thus changing my implementation would break my jUnits, even though my functionality was unaffected).

我正在寻找:


  1. 应该是什么适当使用 Mockito.verify()的指南?

jUnits基本上是正确的吗?知道或紧密耦合到被测试类的实现?

Is it fundamentally correct for jUnits to be aware of, or tightly coupled to, the implementation of the class under test?


推荐答案

如果类A的契约包含它调用类型C的对象的方法B的事实,那么你应该通过模拟类型C并验证方法B已被调用来测试它。

If the contract of class A includes the fact that it calls method B of an object of type C, then you should test this by making a mock of type C, and verifying that method B has been called.

这意味着A类合约的详细信息足以说明C类(可能是接口或类)。所以,是的,我们谈的是超出系统要求的规范水平,并且在某种程度上描述了实现。

This implies that the contract of class A has sufficient detail that it talks about type C (which might be an interface or a class). So yes, we're talking about a level of specification that goes beyond just "system requirements", and goes some way to describing implementation.

单元测试这是正常的。当您进行单元测试时,您需要确保每个单元都在做正确的事情,这通常包括它与其他单元的交互。这里的单位可能意味着您的应用程序的类或更大的子集。

This is normal for unit tests. When you are unit testing, you want to ensure that each unit is doing the "right thing", and that will usually include its interactions with other units. "Units" here might mean classes, or larger subsets of your application.

更新:

我觉得这不仅适用于验证,也适用于存根。一旦你存在一个协作者类的方法,你的单元测试在某种意义上已经变得依赖于实现。单元测试的性质就是如此。由于Mockito与验证有关,因此你使用Mockito这一事实意味着你将遇到这种依赖。

I feel that this doesn't apply just to verification, but to stubbing as well. As soon as you stub a method of a collaborator class, your unit test has become, in some sense, dependent on implementation. It's kind of in the nature of unit tests to be so. Since Mockito is as much about stubbing as it is about verification, the fact that you're using Mockito at all implies that you're going to run across this kind of dependency.

根据我的经验,如果我改变了类的实现,我经常要改变其单元测试的实现来匹配。但是,通常情况下,我不必更改该类 的单元测试的库存;除非当然,改变的原因是存在我之前未能测试的条件。

In my experience, if I change the implementation of a class, I often have to change the implementation of its unit tests to match. Typically, though, I won't have to change the inventory of what unit tests there are for the class; unless of course, the reason for the change was the existence of a condition that I failed to test earlier.

所以这就是单元测试的内容。不受这种对协作者类使用方式的依赖性的测试实际上是子系统测试或集成测试。当然,这些也经常用JUnit编写,并且经常涉及使用模拟。在我看来,JUnit是一个糟糕的名称,对于一种产品,我们可以生产所有不同类型的测试。

So this is what unit tests are about. A test that doesn't suffer from this kind of dependency on the way collaborator classes are used is really a sub-system test or an integration test. Of course, these are frequently written with JUnit too, and frequently involve the use of mocking. In my opinion, "JUnit" is a terrible name, for a product that lets us produce all different types of test.

这篇关于何时使用Mockito.verify()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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