如何使用Mockito thenReturn(Single.just(null))测试null返回

时间:2017-03-31 09:07:49

标签: android tdd mockito rx-android

我想测试单次返回null时的条件

when(repository.getSubLocations(ID)).thenReturn(Single.just(null));

但它给了我错误

at io.reactivex.internal.functions.ObjectHelper.requireNonNull(ObjectHelper.java:39)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,而不是这样做

when(repository.getSubLocations(ID)).thenReturn(Single.just(null));

我这样做

when(repository.getSubLocations(ID)).thenReturn(Single.just(SOME_DATA).map(data -> null));

所以现在我可以成功测试空状态