Mockito:将InOrder与间谍对象一起使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mockito:将InOrder与间谍对象一起使用相关的知识,希望对你有一定的参考价值。
我想使用Mockito检查某些方法的调用顺序。我要检查的一种方法是在模拟程序上,另一种是在我正在测试的真实类中,因此我正在使用间谍对象来检查以下内容:
但是,mockito只知道对模拟方法的调用,而对间谍对象不了解:
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:将InOrder与间谍对象一起使用的主要内容,如果未能解决你的问题,请参考以下文章
是否有可能从MyBatis MapperProxy获取对象?