我正在验证使用Mockito调用的函数,但是Mockito告诉我我正在验证的函数从未被调用过,并且调用了其他函数。但在我看来,我正在调用正确的函数…
下面是我得到的错误的堆栈跟踪:
Wanted but not invoked:
relationshipAutoIndexer.getAutoIndex();
-> at org.whispercomm.manes.server.graph.DataServiceImplTest.testInitIndices(DataServiceImplTest.java:117)
However, there were other interactions with this mock:
-> at org.whispercomm.manes.server.graph.DataServiceImpl.updateIndexProperties(DataServiceImpl.java:136)
-> at org.whispercomm.manes.server.graph.DataServiceImpl.updateIndexProperties(DataServiceImpl.java:144)
-> at org.whispercomm.manes.server.graph.DataServiceImpl.updateIndexProperties(DataServiceImpl.java:148)
-> at org.whispercomm.manes.server.graph.DataServiceImpl.updateIndexProperties(DataServiceImpl.java:149)
-> at org.whispercomm.manes.server.graph.DataServiceImpl.initIndices(DataServiceImpl.java:121)
at org.whispercomm.manes.server.graph.DataServiceImplTest.testInitIndices(DataServiceImplTest.java:117)
出现在
verify(relAutoIndexer).getAutoIndex();
的测试类代码如下所示。
这是我的代码(我有一种偶然遗漏的倾向)。请问我的任何代码,你认为我错过了,我会添加它):
public DataServiceImpl(GraphDatabaseService graphDb) {
super();
this.graphDb = graphDb;
unarchivedParent = new UnarchivedParent(graphDb.createNode());
archivedParent = new ArchivedParent(graphDb.createNode());
packetParent = new PacketParent(graphDb.createNode());
userParent = new UserParent(graphDb.createNode());
this.initIndices();
}
/**
* Initializes the node and relationship indexes.
*
* Updates the set of indexed properties to match {@link DataServiceImpl}
* .NODE_KEYS_INDEXABLE and {@link DataServiceImpl}.REL_KEYS_INDEXABLE.
*
* Note: auto indices can also be configured at database creation time and
* just retrieved at runtime. We might want to switch to that later.
*/
private void initIndices() {
/* Get the auto-indexers */
AutoIndexer<Node> nodeAutoIndexer = this.graphDb.index()
.getNodeAutoIndexer();
AutoIndexer<Relationship> relAutoIndexer = this.graphDb.index()
.getRelationshipAutoIndexer();
this.updateIndexProperties(nodeAutoIndexer,
DataServiceImpl.NODE_KEYS_INDEXABLE);
this.nodeIndex = nodeAutoIndexer.getAutoIndex();
this.updateIndexProperties(relAutoIndexer,
DataServiceImpl.REL_KEYS_INDEXABLE);
this.relIndex = relAutoIndexer.getAutoIndex();
}
/**
* Sets the indexed properties of an {@link AutoIndexer} to the specified
* set, removing old properties and adding new ones.
*
* @param autoIndexer
* the AutoIndexer to update.
* @param properties
* the properties to be indexed.
* @return autoIndexer, this given AutoIndexer (useful for chaining calls.)
*/
private <T extends PropertyContainer> AutoIndexer<T> updateIndexProperties(
AutoIndexer<T> autoIndexer, Set<String> properties) {
Set<String> indexedProps = autoIndexer.getAutoIndexedProperties();
// Remove unneeded properties.
for (String prop : difference(indexedProps, properties)) {
autoIndexer.stopAutoIndexingProperty(prop);
}
// Add new properties.
for (String prop : difference(properties, indexedProps)) {
autoIndexer.startAutoIndexingProperty(prop);
}
// Enable the index, if needed.
if (!autoIndexer.isEnabled()) {
autoIndexer.setEnabled(true);
}
return autoIndexer;
}
下面是测试类的代码:
@Before
public void setup() {
nA = mock(Node.class);
nB = mock(Node.class);
packetA = new PacketWrapper(nA);
packetB = new PacketWrapper(nB);
RelA = mock(Relationship.class);
RelB = mock(Relationship.class);
graphDb = mock(GraphDatabaseService.class);
nodeAutoIndexer = (AutoIndexer<Node>) mock(AutoIndexer.class);
relAutoIndexer = mock(RelationshipAutoIndexer.class);
}
@After
public void tearDown() {
packetA = null;
packetB = null;
}
/*
* ---------------- Test initIndices() ---------------
*/
//TODO
@Test
public void testInitIndices() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
IndexManager indexManager = mock(IndexManager.class);
when(graphDb.index()).thenReturn(indexManager);
when(indexManager.getNodeAutoIndexer()).thenReturn(nodeAutoIndexer);
when(graphDb.index().getRelationshipAutoIndexer()).thenReturn(relAutoIndexer);
dataService = new DataServiceImpl(graphDb);
verify(nodeAutoIndexer, atLeastOnce()).getAutoIndex();
verify(relAutoIndexer).getAutoIndex();
}
Mockito,直到1.8.5版本,在多态调度的情况下有一个错误。它已经修复,并且在1.9.0版本的第一个候选发行版中可用。参见第200期。
那么它是如何在你的代码库中发生的呢?注意,您正在模拟这两个类
nodeAutoIndexer = (AutoIndexer<Node>) mock(AutoIndexer.class);
relAutoIndexer = mock(RelationshipAutoIndexer.class);
AutoIndexer
恰好是一个泛型父接口,在这个接口中有这个方法ReadableIndex<T> getAutoIndex()
。RelationshipAutoIndexer
是AutoInexer
的子类型,其泛型部分固定为Relationship
,覆盖getAutoIndex()
方法返回协变类型ReadableRelationshipIndex
。
参见AutoIndexer和RelationshipIndexer。
嗯,在你的调用代码中,你有这些行:
AutoIndexer<Node> nodeAutoIndexer = this.graphDb.index().getNodeAutoIndexer();
AutoIndexer<Relationship> relAutoIndexer = this.graphDb.index().getRelationshipAutoIndexer();
this.nodeIndex = nodeAutoIndexer.getAutoIndex();
this.relIndex = relAutoIndexer.getAutoIndex();
生产代码中的nodeAutoIndex
和测试代码中的mock nodeAutoIndexer
都有一个类型为AutoIndexer<Node>
的引用,因此在多态调度方面没有问题。但是,生产代码中的relAutoIndex
由类型AutoIndexer<Relationship>
引用,测试代码中的模拟relAutoIndexer
由类型RelationshipAutoIndexer
引用,因此在模拟上注册了错误的调用,然后验证失败。
你的解决方案是要么升级mockito版本;1.9.0 RC1非常稳定,最终版本即将发布。或者您可以将引用类型(在您的生产代码中)从:
迁移到AutoIndexer<Relationship> relAutoIndexer = this.graphDb.index().getRelationshipAutoIndexer();
to:
RelationshipAutoIndexer relAutoIndexer = this.graphDb.index().getRelationshipAutoIndexer();
还有几句话。
您实际上不需要在这里编写after方法,因为JUnit在每个方法运行时都会创建一个新实例,因此您的方法只是添加了无论如何都会完成的代码。注意,TestNG不是这样的。
您可能希望使用Mockito注释,而不是在before方法中创建mock。别忘了跑步者。
例如:
@RunWith(MockitoJUnitRunner.class)
public class YourTest {
@Mock SomeType someTypeMock;
// ...
}
由于几个原因,存根代码有点难看。
- 你应该写一致的存根
为什么不以一种更简洁的方式来写呢?例如,在两种情况下都引用indexManager
:
IndexManager indexManager = mock(IndexManager.class);
when(graphDb.index()).thenReturn(indexManager);
when(indexManager.getNodeAutoIndexer()).thenReturn(nodeAutoIndexer);
when(indexManager.getRelationshipAutoIndexer()).thenReturn(relAutoIndexer);
或者根本不引用
IndexManager indexManager = mock(IndexManager.class);
when(graphDb.index()).thenReturn(indexManager);
when(graphDb.index().getNodeAutoIndexer()).thenReturn(nodeAutoIndexer);
when(graphDb.index().getRelationshipAutoIndexer()).thenReturn(relAutoIndexer);
拥有一个返回一个mock的mock通常是设计异味的标志。您违反了Demeter法则,而违反它意味着您将经历困难的测试、糟糕的可维护性和困难的进化。当我这样说的时候,你也能听到我的低语(没有三段论):这将花费你的钱。不要写遗留代码!如果您正在实践TDD或BDD,您将在设计时为自己的代码识别这些问题,这对于防止它们非常有用。
- 然而,如果你正在处理遗留代码,你可以使用这种深存根语法:
使用静态方法你可以这样写
GraphDatabaseService graphdb = mock(GraphDatabaseService.class, RETURNS_DEEP_STUBS);
或者使用注释你可以这样写:
@Mock(answer = RETURNS_DEEP_STUBS) GraphDatabaseService graphdb;
和存根:
when(graphDb.index().getNodeAutoIndexer()).thenReturn(nodeAutoIndexer);
when(graphDb.index().getRelationshipAutoIndexer()).thenReturn(relAutoIndexer);