Mockito:将 InOrder 与间谍对象一起使用
Mockito: use InOrder with spy object
我想使用 mockito 检查某些方法的调用顺序。我想检查的方法之一是模拟,另一个是我正在测试的真实 class,所以我使用间谍对象来检查:
然而,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));
我能做什么?发生什么事了?
解决方案是调用间谍对象而不是真实对象:
migrationServiceSpy.updateStatus(DEFAULT_MIGRATION_ID, inProgressStatus);
而不是:
migrationServiceImpl.updateStatus(DEFAULT_MIGRATION_ID, inProgressStatus);
我想使用 mockito 检查某些方法的调用顺序。我想检查的方法之一是模拟,另一个是我正在测试的真实 class,所以我使用间谍对象来检查:
然而,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));
我能做什么?发生什么事了?
解决方案是调用间谍对象而不是真实对象:
migrationServiceSpy.updateStatus(DEFAULT_MIGRATION_ID, inProgressStatus);
而不是:
migrationServiceImpl.updateStatus(DEFAULT_MIGRATION_ID, inProgressStatus);