我想使用 mockito 检查某些方法的调用顺序。我想检查的方法之一是在模拟上,另一个是在我正在测试的真实类中,所以我使用间谍对象来检查:
然而,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));
我能做什么?这是怎么回事?
解决方案是调用 spy 对象而不是真实对象:
migrationServiceSpy.updateStatus(DEFAULT_MIGRATION_ID, inProgressStatus);
代替:
migrationServiceImpl.updateStatus(DEFAULT_MIGRATION_ID, inProgressStatus);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句