检查模拟对象的类类型

发布于 2024-11-14 09:39:53 字数 482 浏览 3 评论 0原文

我正在测试一个方法,该方法获取一个对象并检查该对象是否是存储为实例变量的类的实例。到目前为止没有问题。

但在测试中我必须使用模拟,并且这些模拟之一是传递给该方法的对象。而现在,事情变得棘手了。我们来看看代码(我总结了这个测试中的代码):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

本宫微胖 2024-11-21 09:39:53

您的第一个断言将永远为真 - Mockito 模拟是一个全新的类,因此简单的 equals() 永远不会起作用。顺便说一句,对于这样的测试,如果您使用 Assert.assertEquals() ,您将收到更有用的失败消息,其中第一个参数是预期结果;例如:

Assert.assertEquals(clazz, adapterEvent.getClass()); 

你的第二个断言是正确的,但是你混淆了isAssignableFrom()的方向(很容易完成,JavaDoc非常令人困惑) - 翻转它并你是金子:

Assert.assertTrue(clazz.isAssignableFrom(adapterEvent.getClass()));

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 use Assert.assertEquals(), where the first argument is the expected result; e.g.:

Assert.assertEquals(clazz, adapterEvent.getClass()); 

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:

Assert.assertTrue(clazz.isAssignableFrom(adapterEvent.getClass()));
墟烟 2024-11-21 09:39:53

有一个新方法 getMockedType Mockito 2.0.0 返回最初传入 Mockito.mock(Class) 的类。我建议使用此方法,因为 getSuperClass() 技术并非在所有情况下都有效。

MockingDetails mockingDetails = Mockito.mockingDetails(mockObj);
Class<?> cls = mockingDetails.getMockedType();

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 the getSuperClass() technique doesn't work in all cases.

MockingDetails mockingDetails = Mockito.mockingDetails(mockObj);
Class<?> cls = mockingDetails.getMockedType();
浊酒尽余欢 2024-11-21 09:39:53

我认为instanceof会按照你想要的方式工作:

Assert.assertTrue(adapterEvent instanceof AdapterEvent);

你确定你应该对此进行测试吗?不知道你想要完成什么,很难说,但我认为这个测试可能没有必要。

I would think that instanceof would work the way you want it to:

Assert.assertTrue(adapterEvent instanceof AdapterEvent);

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.

迎风吟唱 2024-11-21 09:39:53

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));

爱的故事 2024-11-21 09:39:53

要测试一个对象是否已返回您期望的 C# 类的实例,请执行以下操作

Assert.IsInstanceOfType(adapterEvent, typeof(AdapterEvent));

To test an object has returned the instance of a class in C# you are expecting then do the following

Assert.IsInstanceOfType(adapterEvent, typeof(AdapterEvent));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文