检查模拟对象的类类型
我正在测试一个方法,该方法获取一个对象并检查该对象是否是存储为实例变量的类的实例。到目前为止没有问题。
但在测试中我必须使用模拟,并且这些模拟之一是传递给该方法的对象。而现在,事情变得棘手了。我们来看看代码(我总结了这个测试中的代码):
Class<AdapterEvent> clazz = AdapterEvent.class;
AdapterEvent adapterEvent = Mockito.mock(AdapterEvent.class);
Assert.assertTrue(adapterEvent.getClass().equals(clazz));
Assert.assertTrue(adapterEvent.getClass().isAssignableFrom(clazz));
嗯,这个测试实际上失败了。有谁知道为什么?有人知道如何通过仍然使用测试中的模拟来解决这个问题吗?是否有另一种方法可以将对象与特定类进行比较。
I'm testing a method that gets an object and checks if that object is an instance of a class that is stored as instance variable. So far no problem.
But in the test I have to use mocks and one of these mocks is the object that is passed on to that method. And now, it becomes tricky. Let's see the code (I summed up the code in this test):
Class<AdapterEvent> clazz = AdapterEvent.class;
AdapterEvent adapterEvent = Mockito.mock(AdapterEvent.class);
Assert.assertTrue(adapterEvent.getClass().equals(clazz));
Assert.assertTrue(adapterEvent.getClass().isAssignableFrom(clazz));
Well, this test actually fails. Does anyone know why? Does somebody has an idea how I could solve this problem by still using a mock like in the test? Is there maybe another way of comparing objects to a specific class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的第一个断言将永远为真 - Mockito 模拟是一个全新的类,因此简单的
equals()
永远不会起作用。顺便说一句,对于这样的测试,如果您使用 Assert.assertEquals() ,您将收到更有用的失败消息,其中第一个参数是预期结果;例如:你的第二个断言将是正确的,但是你混淆了
isAssignableFrom()
的方向(很容易完成,JavaDoc非常令人困惑) - 翻转它并你是金子:Your first assertion will never be true - Mockito mocks are a whole new class, so a simple
equals()
will never work. By the way, for tests like this you'll get a far more useful failure message if you useAssert.assertEquals()
, where the first argument is the expected result; e.g.:Your second assertion would be correct, but you've mixed up the direction of
isAssignableFrom()
(easily done, the JavaDoc is mighty confusing) - flip it around and you're golden:有一个新方法 getMockedType Mockito 2.0.0 返回最初传入
Mockito.mock(Class)
的类。我建议使用此方法,因为getSuperClass()
技术并非在所有情况下都有效。There is a new method getMockedType in Mockito 2.0.0 that returns the class originally passed into
Mockito.mock(Class)
. I would recommend using this method because thegetSuperClass()
technique doesn't work in all cases.我认为instanceof会按照你想要的方式工作:
你确定你应该对此进行测试吗?不知道你想要完成什么,很难说,但我认为这个测试可能没有必要。
I would think that instanceof would work the way you want it to:
Are you sure you should even be testing for this? Not knowing what you're trying to accomplish it's hard to say but I think this test might not be necessary.
Mocked 类是原始类的子类,因此只需检查超类,如下所示:
Assert.assertTrue(adapterEvent.getClass().getSuperclass().equals(clazz));
The Mocked class is subclassed from your original class, so just check the superclass, like this:
Assert.assertTrue(adapterEvent.getClass().getSuperclass().equals(clazz));
要测试一个对象是否已返回您期望的 C# 类的实例,请执行以下操作
To test an object has returned the instance of a class in C# you are expecting then do the following