Mockito.mock 做了什么以及为什么在摆脱 Mockito 时该实现不起作用的建议
我有以下接口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
创建给定类或接口的模拟对象,在本例中,CatalogVersionService
由DefaultCatalogVersionService
实现。因此,当我尝试获取真实对象时,我会执行以下操作:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,假设
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 yourDefaultCatalogVersionService
. 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 theDefaultCatalogVersionService
!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.