首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mockito:对间谍对象使用InOrder

Mockito:对间谍对象使用InOrder
EN

Stack Overflow用户
提问于 2020-06-11 23:33:34
回答 1查看 214关注 0票数 0

我想用mockito检查一些方法的调用顺序。我想要检查的方法之一是在模拟上,另一个是在我正在测试的真实类中,所以我使用一个spy对象来检查:

但是,mockito只知道对mock方法的调用,而不知道对spy对象的调用:

代码语言:javascript
复制
    MigrationServiceImpl migrationServiceSpy = spy(migrationServiceImpl);

    // I have tested without no more configuraitons on the spy, and also with the following
    // by separate (one by one)

    // I tried this:
    doNothing().when(migrationServiceSpy).updateCheckpointDate(eq(migration), any(Instant.class)); // In this case the call is not tracked by InOrder
    // And this:
    when(migrationServiceSpy.updateCheckpointDate(eq(migration), any(Instant.class))).thenReturn(true); // In this case the call is not tracked by InOrder
    // And this:
    when(migrationServiceSpy.updateCheckpointDate(eq(migration), any(Instant.class))).thenCallRealMethod(); // This line is throwing a null pointer exception but I don't understand why, since  if I do not spy the real object it works without failing.

    //Here I call the real method
    migrationServiceImpl.updateStatus(DEFAULT_MIGRATION_ID, inProgressStatus);

    InOrder inOrder = inOrder(migrationServiceSpy, mongoMigrationRunner);

    inOrder.verify(migrationServiceSpy).updateCheckpointDate(any(Migration.class), any(Instant.class));
    inOrder.verify(mongoMigrationRunner).runMigrationForInterval(any(Migration.class), anyString(), any(Instant[].class));

我能做什么?怎么回事?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-12 14:51:13

解决方案是调用spy对象而不是真实对象:

代码语言:javascript
复制
migrationServiceSpy.updateStatus(DEFAULT_MIGRATION_ID, inProgressStatus);

而不是:

代码语言:javascript
复制
migrationServiceImpl.updateStatus(DEFAULT_MIGRATION_ID, inProgressStatus);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62327995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档