Mockito - 存根模拟对象方法返回的对象的方法
假设我有一个模拟对象,并且我不想存根它的任何方法,但我想存根它返回的对象的方法。例如,
when(mockObject.method1()).thenReturn(returnValue)
通常是这样做的,但我正在寻找,
when(mockObject.method1().method2()).thenReturn(returnValue)
这可能吗?如果我这样做,我会得到一个 NullPointerException 异常。目前我已经存根第一个方法来返回模拟对象,然后使用返回的模拟对象存根第二个方法。然而,这些临时模拟对象对我来说毫无用处,并且在将许多方法链接在一起之后,导致了许多无用的模拟对象。
编辑:实际上,链接可能有效,但我的对象导致了 NPE。此代码(第一行)导致 NPE:
when(graphDb.index().getNodeAutoIndexer()).thenReturn(nodeAutoIndexer);
when(graphDb.index().getRelationshipAutoIndexer()).thenReturn(relAutoIndexer);
但此代码有效:
IndexManager indexManager = mock(IndexManager.class);
when(graphDb.index()).thenReturn(indexManager);
when(indexManager.getNodeAutoIndexer()).thenReturn(nodeAutoIndexer);
when(graphDb.index().getRelationshipAutoIndexer()).thenReturn(relAutoIndexer);
因此,链接对返回 AutoIndexer 对象的 getNodeAutoIndexer() 不起作用,而对返回 RelationshipAutoIndexer 的 getRelationshipAutoIndexer() 有效。两个返回值都被模拟如下:
nodeAutoIndexer = (AutoIndexer<Node>) mock(AutoIndexer.class);
relAutoIndexer = mock(RelationshipAutoIndexer.class);
那么是什么原因导致了这个问题呢?
Let's say I have an mock object, and I don't want to stub any of it's methods, but I want to stub a method of an object it returns. For example,
when(mockObject.method1()).thenReturn(returnValue)
is how it's normally done, but I'm looking for,
when(mockObject.method1().method2()).thenReturn(returnValue)
Is that possible? I get a NullPointerException if I do that. Currently I have stub the first method to return a mock object, and then using that returned mock object, stub the second method. However, these temporary mock objects are useless to me and after chaining many methods together, that results in a lot of useless mock objects.
EDIT: Actually, it's possible that chaining works, but my objects are causing the NPE. This code (the first line) is causing a NPE:
when(graphDb.index().getNodeAutoIndexer()).thenReturn(nodeAutoIndexer);
when(graphDb.index().getRelationshipAutoIndexer()).thenReturn(relAutoIndexer);
But this code works:
IndexManager indexManager = mock(IndexManager.class);
when(graphDb.index()).thenReturn(indexManager);
when(indexManager.getNodeAutoIndexer()).thenReturn(nodeAutoIndexer);
when(graphDb.index().getRelationshipAutoIndexer()).thenReturn(relAutoIndexer);
So chaining didn't work for getNodeAutoIndexer() which returns an AutoIndexer object while it worked for getRelationshipAutoIndexer() which returns a RelationshipAutoIndexer. Both return values are mocked as follows:
nodeAutoIndexer = (AutoIndexer<Node>) mock(AutoIndexer.class);
relAutoIndexer = mock(RelationshipAutoIndexer.class);
So what could be causing the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
完全没有问题。
让我们检查一下这 4 行代码:
第一行创建一个模拟的 indexManager。
第二个告诉模拟 graphDb 在调用 index 方法时返回 indexManager (在第一行创建的模拟)。
第三个告诉模拟indexManager(在第一行创建)在调用其getNodeAutoIndexer 方法时返回nodeAutoIndexer。
最后一行调用 graphDb.index(),它返回模拟indexManager(您在第二行告诉它这样做),并要求该indexManager(这是您在第一行创建的模拟)在其 getRelationshipAutoIndexer 方法时返回relAutoIndexer被称为。
最后一行之所以有效,是因为您告诉模拟 graphDb 在调用其索引方法时要返回什么。如果您之前没有这样做,则模拟 graphDb.index() 方法将返回 null,并且您将获得 NPE。
There is no problem at all.
Let's examine these 4 lines of code:
The first line creates a mock indexManager.
The second one tells the mock graphDb to return indexManager (the mock created at first line) when the index method is called.
The third one telle the mock indexManager (created at first line) to return nodeAutoIndexer when its getNodeAutoIndexer method is called.
And the last line calls graphDb.index(), which returns the mock indexManager (you told it to do that at line two), and asks this indexManager (which is the mock you created at first line) to return relAutoIndexer when its getRelationshipAutoIndexer method is called.
The last line works only because you told the mock graphDb what to return when its index method is called. If you had not done this before, the mock graphDb.index() method would have returned null and you would have had an NPE.