Mocking values in TDD

早过忘川 提交于 2019-12-02 01:34:57

Not all immutable objects are value objects. By the way, when designing, consider that the ideal object has only immutable fields and no-arg methods.

Regarding the heuristic, a valid approach can be considering how objects will be used: if you build an instance, invoke some methods and then are done with it (or store it in a field) likely it won't be a value object. On the contrary, if you keep objects in some data structure and compare them (with .equals()) likely you have a value object. This is especially true for objects that will be used to key Maps

Value objects should be automatic-tested themselves (and tests are usually a pleasure to read and write because are straightforward) but there's no point in mocking them: the main practical reasons for mocking interfaces is that implementation classes

  • are usually difficult to build (need lot of collaborators)
  • are expensive to run (access the network, the filesystem, ...).

Neither apply to value objects.

Quoting the linked blog post:

There are a couple of heuristics for when a class is not worth mocking. First, it has only accessors or simple methods that act on values it holds, it doesn't have any interesting behaviour. Second, you can't think of a meaningful name for the class other than VideoImpl or some such vague term.

The implication of the first point, in the context of a section entitled "Don't mock value objects", is that value objects don't have interesting behaviour.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!