Jea*_*ôté 5 junit unit-testing mocking junit4 mockito
我正在编写一些单元测试,却偶然发现了这个奇怪的“ bug”,这使我无法运行单元测试。
当我运行“ 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
}
Run Code Online (Sandbox Code Playgroud)
这是我的setUp()和单元类测试的成员部分
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);
}
Run Code Online (Sandbox Code Playgroud)
我不知道它是否相关,但是BrandSyncUpdater和StationSyncUpdater都有一个名为SyncUpdater的父级,isDone()函数位于该父级。
编辑
班级等级
Updater 独自一堂课
BrandSyncUpdater并且StationSyncUpdater正在扩展SyncUpdater
Updater isDone() 签名和代码:
public boolean isDone() {
return states.isEmpty();
}
Run Code Online (Sandbox Code Playgroud)
SyncUpdater isDone() 签名和代码:
public boolean isDone() {
return currentStates.isEmpty();
}
Run Code Online (Sandbox Code Playgroud)
编辑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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7401 次 |
| 最近记录: |