Mockito.mock 做了什么以及为什么在摆脱 Mockito 时该实现不起作用的建议

发布于 2024-11-07 23:26:15 字数 1420 浏览 0 评论 0原文

我有以下接口CatalogVersionService,它公开了一些服务。我还有一个单元测试,它通过使用 Mockito 来模拟此接口,如下所示:

CatalogVersionService catalogVersionService  = Mockito.mock(CatalogVersionService.class);

并将 catalogVersionService 注入名为 DefaultClassificationClassResolverService 的解析器实现中,如下所示:

((DefaultClassificationClassResolverService) ccrservice).setCatalogVersionService(catalogVersionService);

// Assert that my resolver will find a single ClassificationClassModel object
ClassificationClassModel single = new ClassificationClassModel();
        assertTrue(ccrservice.resolve(single).contains(single)); //resolver

到目前为止,一切正常很好,直到我尝试创建集成测试并摆脱模拟的 CatalogVersionService 接口。据我所知,Mockito.mock 创建给定类或接口的模拟对象,在本例中,CatalogVersionServiceDefaultCatalogVersionService 实现。因此,当我尝试获取真实对象时,我会执行以下操作:

catalogVersionService = new DefaultCatalogVersionService();
((DefaultClassificationClassResolverService) ccrservice).setCatalogVersionService(catalogVersionService);

但是,在那之后我会收到空​​指针异常,并且我的解析器测试当然会失败。那么 Mockito.mock 到底做了什么?这是一个很好的假设方法:

CatalogVersionService catalogVersionService  = Mockito.mock(CatalogVersionService.class);
// IS EQUIVALENT TO:
catalogVersionService = new DefaultCatalogVersionService();

对于断言失败的原因有什么想法吗?

提前致谢

I have the following interface CatalogVersionService which exposes some services. As well I have a unit test which mocks this interface by using Mockito like this:

CatalogVersionService catalogVersionService  = Mockito.mock(CatalogVersionService.class);

And injects the catalogVersionService in a resolver implementation named DefaultClassificationClassResolverService like this:

((DefaultClassificationClassResolverService) ccrservice).setCatalogVersionService(catalogVersionService);

// Assert that my resolver will find a single ClassificationClassModel object
ClassificationClassModel single = new ClassificationClassModel();
        assertTrue(ccrservice.resolve(single).contains(single)); //resolver

Up to that point everything works fine until I try to create an integration test and get rid of the Mocked CatalogVersionService interface. As far as I am aware Mockito.mock creates a mock object of given class or interface, in this case CatalogVersionService which is implemented by DefaultCatalogVersionService. So when I try to obtain the real object I do something like this:

catalogVersionService = new DefaultCatalogVersionService();
((DefaultClassificationClassResolverService) ccrservice).setCatalogVersionService(catalogVersionService);

However, after that point is where I get a null pointer exception and my resolver test of course fails. So what does Mockito.mock actually do?? Is it a good approach to assume:

CatalogVersionService catalogVersionService  = Mockito.mock(CatalogVersionService.class);
// IS EQUIVALENT TO:
catalogVersionService = new DefaultCatalogVersionService();

Any ideas why the assert is failing?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

万水千山粽是情ミ 2024-11-14 23:26:15

不,假设 Mockito.mock(...) 与实例化 DefaultCatalogVersionService 实例相同是不正确的。这不是模拟所做的。

如果您在使用具体的 DefaultCatalogVersionService 时遇到 NullPointerException,则表明 DefaultCatalogVersionService 中的某些内容为空!

您是否检查过堆栈跟踪以查看 NullPointerException 发生在哪一行,这将帮助您确定哪个属性/字段为空?

您的 DefaultCatalogVersionService 很可能依赖于其他类,而您没有在测试中连接这些类。

No, it is incorrect to assume that Mockito.mock(...) is the same as instantiating an instance of your DefaultCatalogVersionService. This is not what mocks do.

If you are getting a NullPointerException when using the concrete DefaultCatalogVersionService, this would suggest that something is null within the DefaultCatalogVersionService!

Have you examined the stacktrace to see at what line the NullPointerException occurs, which would help you determine which property/field is null?

It's more than likely that your DefaultCatalogVersionService depends on other classes, which you are not wiring up in your test.

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