我想用mockito检查一些方法的调用顺序。我想要检查的方法之一是在模拟上,另一个是在我正在测试的真实类中,所以我使用一个spy对象来检查:
但是,mockito只知道对mock方法的调用,而不知道对spy对象的调用:
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));我能做什么?怎么回事?
发布于 2020-06-12 14:51:13
解决方案是调用spy对象而不是真实对象:
migrationServiceSpy.updateStatus(DEFAULT_MIGRATION_ID, inProgressStatus);而不是:
migrationServiceImpl.updateStatus(DEFAULT_MIGRATION_ID, inProgressStatus);https://stackoverflow.com/questions/62327995
复制相似问题