Mockito - “通缉但未调用;但是还有其他与此模拟的交互”错误

时间:2011-10-31 02:43:30

标签: java junit mockito verify

我正在验证是否使用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();                       
}

1 个答案:

答案 0 :(得分:25)

Mockito,直到版本1.8.5,在多态调度的情况下有一个错误。它已修复,可在1.9.0版的第一个候选版本中使用。请参阅issue 200

那么在你的代码库中它是如何发生的。请注意,您正在嘲笑这两个类

nodeAutoIndexer = (AutoIndexer<Node>) mock(AutoIndexer.class);
relAutoIndexer = mock(RelationshipAutoIndexer.class);

AutoIndexer碰巧是一个通用的父接口,在这个接口中有这个方法ReadableIndex<T> getAutoIndex()RelationshipAutoIndexerAutoInexer的子类型,其中通用部分固定为Relationship,并覆盖getAutoIndex()方法以返回协变类型ReadableRelationshipIndex

请参阅AutoIndexerRelationshipIndexer

好吧,在您的通话代码中,您有以下几行:

AutoIndexer<Node> nodeAutoIndexer = this.graphDb.index().getNodeAutoIndexer();
AutoIndexer<Relationship> relAutoIndexer = this.graphDb.index().getRelationshipAutoIndexer();
this.nodeIndex = nodeAutoIndexer.getAutoIndex();
this.relIndex = relAutoIndexer.getAutoIndex();

生产代码中的nodeAutoIndex和测试代码中的模拟nodeAutoIndexer都有AutoIndexer<Node>类型的引用,因此多态分派没有问题。 但是,生产代码中的relAutoIndex由类型AutoIndexer<Relationship>引用,并且测试代码中的模拟relAutoIndexer由类型RelationshipAutoIndexer引用,因此错误的调​​用已在模拟,然后验证失败。

您的解决方案要么升级mockito版本; 1.9.0 RC1非常稳定,最终版本应该是你的方式。 或者您可以从以下网址迁移参考类型(在您的生产代码中)

AutoIndexer<Relationship> relAutoIndexer = this.graphDb.index().getRelationshipAutoIndexer();

到:

RelationshipAutoIndexer relAutoIndexer = this.graphDb.index().getRelationshipAutoIndexer();

其他一些评论。

  • 您实际上不需要在此处编写after方法,因为JUnit会在每个方法运行时创建一个新实例,因此您的方法只会添加将要执行的代码。请注意,这不是TestNG的情况。

  • 您可能希望使用Mockito注释,而不是在before方法中创建模拟。不要忘记跑步者。

例如:

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

还有一个返回模拟的模拟通常是设计气味的标志。你违反了得墨忒耳的法则,打破它意味着你将经历艰难的测试,糟糕的可维护性和难以进化。当我说你也可以听到我耳语(没有三段论):它会花费你的钱。不要写遗留代码!如果您正在练习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);