我正在写一些单元测试,我偶然发现了这个奇怪的“bug”,它阻止了我运行我的单元测试。模拟Mockito返回一个InvocationTargetException
当我运行“when(...)。thenReturn(...)”时,我收到一个InvocationTargetException。然后奇怪的是,当我调试时,它会进入真实的对象并崩溃在一个空成员。当我调试其他的“when”时,它会出现一个名为“Intercept”的函数,它阻止了真正的代码......我不明白这个对象有什么不同以及如何防止这种奇怪的行为。
这里是我的单元测试:
@Test
public void getSyncStatusShouldReturnValueFromDiskWhenNotRunning() throws IOException {
//Arrange
when(updater.isDone()).thenReturn(true);
when(brandSyncUpdater.isDone()).thenReturn(true); //This is where it throw error
when(stationSyncUpdater.isDone()).thenReturn(true);
//Act
//Assert
}
这里是我的设置()和我的单位等级测试
private Updater updater;
private BrandSyncUpdater brandSyncUpdater;
private StationSyncUpdater stationSyncUpdater;
@Before
public void setUp() {
updater = mock(Updater.class);
brandSyncUpdater = mock(BrandSyncUpdater.class);
stationSyncUpdater = mock(StationSyncUpdater.class);
}
的成员部分,我不知道它是否有关,但BrandSyncUpdater和StationSyncUpdater在isDone()函数所在的位置都有一个名为SyncUpdater的父级。
编辑
类
Updater
的层次是对自己
BrandSyncUpdater
和StationSyncUpdater
正在扩展SyncUpdater
Updater isDone()
SI类gnature和代码:
public boolean isDone() {
return states.isEmpty();
}
SyncUpdater isDone()
签名和代码:
public boolean isDone() {
return currentStates.isEmpty();
}
编辑2
这里是在控制台中错误的堆栈跟踪。你会注意到这里的错误是一个“NullPointerException”,因为它试图使用变量currentStates
。但是在调试时,mockito抛出的错误是InvocationTargetException。
java.lang.NullPointerException
at com.stingray360.clap.synchronizer.contentcontroller.queue.SyncUpdater.isDone(SyncUpdater.java:117)
at com.stingray360.clap.synchronizer.contentcontroller.queue.BrandSyncUpdater.isDone(BrandSyncUpdater.java:15)
at com.stingray360.clap.synchronizer.contentcontroller.SyncDispatcherTest.getSyncStatusShouldReturnValueFromDiskWhenNotRunning(SyncDispatcherTest.java:190)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
你可以发布'BrandSyncUpdater'和'Updater'类吗? – Ruben
我不能,因为它是我工作中的代码,但如果你告诉我你想知道的事情,我可以发布一些部分或/和添加信息到我的问题。就像我说的,Updater工作正常,它是在嘲笑时抛出错误的BrandSyncUpdater和StationSyncUpdater。 –
类的方法和层次结构的签名将会很有帮助 – Ruben