防止存根等于方法

防止存根等于方法

本文介绍了防止存根等于方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试我的类的equals()方法,但是Mockito似乎每次都在调用存根版本.我的测试如下;

I would like to test my class' equals() method but Mockito seems to be calling the stub version every time. My test is as follows;

PluginResourceAdapter adapter = mock (PluginResourceAdapter.class);
PluginResourceAdapter other = mock (PluginResourceAdapter.class);

when(adapter.getNumberOfEndpointActivation()).thenReturn(1);
when(other.getNumberOfEndpointActivation()).thenReturn(0);

boolean result = adapter.equals(other);
assertFalse(result);

我知道我无法对equals方法进行存根处理,这意味着Mockito应该调用我的实际实现,但不是.

I know I cannot stub the equals method which means Mockito should be calling my real implementation but its not.

我也尝试过:

when (adapter.equals(any()).thenCallRealMethod()

但我得到相同的结果.

推荐答案

甚至超出了 Mockito的局限性,如果模拟对象仅出于其他原因(几乎等于方法总是使用字段),那么使用一个真正的equals方法就没有多大意义.模拟对象永远不会运行其构造函数或字段初始化程序.

Even beyond Mockito's limitations, it doesn't make much sense for a mocked object to use a real equals method, if for no other reason than that equals methods almost always use fields, and mocked objects never run any of their constructors or field initializers.

此外,请注意要测试的内容:在测试Foo时,理想情况下,您永远不要模拟Foo,甚至不要设置要与之比较的Foo.否则,很容易无意间测试Mockito的工作原理,而不是测试自己组件的逻辑.

Also, be aware of what you're testing: In a test of Foo, ideally you should never mock Foo, even to set up a Foo to compare against. Otherwise, it's easy to inadvertently test that Mockito works, rather than testing your own component's logic.

您有一些解决方法:

  • 正如Garrett Hall所说,创建真实的对象.这可能需要从使用它们的服务中剔除数据对象",并在使用实际数据对象时对服务进行模拟.总体来说,这可能是个好主意.

  • As Garrett Hall mentioned, create real objects. This may require factoring out the "data objects" from the services that use them, and mocking the services while using real data objects. This is probably a good idea overall.

通过子类化PluginResourceAdapter或在Mockito外部实现相关接口来创建手动模拟或伪造.这样您就可以根据需要定义所有方法,包括equalshashCode.

Create a manual mock or fake by subclassing PluginResourceAdapter or implementing the relevant interface outside of Mockito. This frees you to define all methods as needed, including equals and hashCode.

创建一个equivalentTo方法,该方法与equals不同(例如,它对Map或Set对象的用处不大),但具有可在您自己的对象上定义的可模仿语义自己的.

Create an equivalentTo method, which isn't the same as equals (and thus isn't as useful for Map or Set objects, for instance) but that has mockable semantics you can define on your own.

这还使您可以使用模拟程序自由测试equivalentTo,只需将equals委托给该经过良好测试的实现即可.

This would also let you test equivalentTo freely with a mock, and simply have equals delegate to that presumably-well-tested implementation.

提取一个测试相等性的对象,然后对其进行模拟.您还可以使用 Guava的Equivalence 或在Comparator中测试a.compareTo(b) == 0的地方.

Extract an object that tests equality, and mock that. You could also use Guava's Equivalence there, or a Comparator where you test a.compareTo(b) == 0.

class YourClass {
  class AdapterEquivalence {
    boolean adaptersAreEqual(
        PluginResourceAdapter a, PluginResourceAdapter b) {
      return a.equals(b);
    }
  }

  /** Visible for testing. Replace in tests. */
  AdapterEquivalence adapterEquivalence = new AdapterEquivalence();
}

请注意,另一种可能的解决方法-监视现有实例-也将重新定义equalshashCode 并不会在这里为您提供帮助.

Note that one other potential workaround—spying on existing instances—will also redefine equals and hashCode and won't help you here.

这篇关于防止存根等于方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:37