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

2022-09-03 12:54:47

我正在验证使用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

Mockito,直到1.8.5版本,在多态调度的情况下有一个错误。它已修复,并在版本 1.9.0 的第一个候选发行版中可用。第200期

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

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

AutoIndexer碰巧是通用父接口,在此接口中有此方法 。 是 泛型部分固定为 的 子类型,并覆盖该方法以返回协变类型 。ReadableIndex<T> getAutoIndex()RelationshipAutoIndexerAutoInexerRelationshipgetAutoIndex()ReadableRelationshipIndex

请参阅自动索引器关系索引器

好吧,在您的调用代码中,您有以下行:

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

在生产代码中和测试代码中的模拟中都有一个类型的引用,因此多态调度没有问题。但是,在生产代码中,由类型引用,而测试代码中的模拟由类型引用,因此在模拟上注册了错误的调用,然后验证失败。nodeAutoIndexnodeAutoIndexerAutoIndexer<Node>relAutoIndexAutoIndexer<Relationship>relAutoIndexerRelationshipAutoIndexer

您的解决方案是升级模拟版本;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);

同样,有一个返回模拟的模拟通常是设计气味的标志。你正在打破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);

答案 2

推荐