我正在尝试对viewModel类进行单元测试,但是当我运行测试时,在我的一次性OnSuccess方法中得到了NullPointerException,我不明白为什么。因此,我测试的方法总是返回null。
这是我的测试课代码:
CityListViewModelTest.kt
@RunWith(JUnit4::class)
class CityListViewModelTest {
@Rule
@JvmField
val rule = InstantTaskExecutorRule()
@Mock
private lateinit var repository: ForecastRepository
@InjectMocks
private lateinit var viewModel: CityListViewModel
@Before @Throws fun setUp(){
RxAndroidPlugins.setInitMainThreadSchedulerHandler{Schedulers.trampoline()}
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
MockitoAnnotations.initMocks(this)
}
@Test
fun getCities() {
val response = getMockedCities(5)
`when`(repository.getCities(ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble()))
.thenReturn(Single.just(response))
val result = viewModel.getCities(0.0,0.0)
verify(repository).getCities(0.0,0.0)
verify(repository).getCache() //should be called but isn't
assertEquals(response.list,result.value) //result.value should be a list of 5 mocked cities but is null
}
fun getMockedCities(count : Int) : OpenWeatherCycleDataResponse {
val cities = ArrayList<City>()
for (i in 0..count) {
val city = mock(City::class.java)
cities.add(city)
}
return OpenWeatherCycleDataResponse(cities)
}
}
还有我的viewModel类:
CityListViewModel.kt
class CityListViewModel @Inject constructor(private var forecastRepo: ForecastRepository):ViewModel() {
//@Inject lateinit
var cities : MutableLiveData<List<City>> = MutableLiveData()
//@Inject lateinit
var disposable : CompositeDisposable = CompositeDisposable()
fun getCities(lat: Double,lon:Double): LiveData<List<City>> {
disposable.add(forecastRepo.getCities(lat,lon).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object: DisposableSingleObserver<OpenWeatherCycleDataResponse>(){
override fun onSuccess(t: OpenWeatherCycleDataResponse) {
forecastRepo.getCache().saveCities(t.list)
cities.value = t.list
}
override fun onError(e: Throwable) {
Timber.e(e.localizedMessage)
}
}))
return cities
}
fun getCityByName(cityName: String): LiveData<City>{
val searchedCity = MutableLiveData<City>()
disposable.add(forecastRepo.getCityByName(cityName).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object: DisposableSingleObserver<City>(){
override fun onSuccess(t: City) {
searchedCity.value = t
forecastRepo.getCache().saveCities(listOf(t))
}
override fun onError(e: Throwable) {
Timber.e(e.localizedMessage)
}
}))
return searchedCity
}
override fun onCleared() {
super.onCleared()
disposable.clear()
}
}
以下是日志:
java.lang.NullPointerException
at com.example.zach.weatherapp.viewModel.CityListViewModel$getCities$1.onSuccess(CityListViewModel.kt:30)
at com.example.zach.weatherapp.viewModel.CityListViewModel$getCities$1.onSuccess(CityListViewModel.kt:27)
at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:81)
at io.reactivex.internal.schedulers.TrampolineScheduler.scheduleDirect(TrampolineScheduler.java:52)
at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.onSuccess(SingleObserveOn.java:64)
at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.onSuccess(SingleSubscribeOn.java:68)
at io.reactivex.internal.operators.single.SingleJust.subscribeActual(SingleJust.java:30)
at io.reactivex.Single.subscribe(Single.java:3096)
at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
at io.reactivex.internal.schedulers.TrampolineScheduler.scheduleDirect(TrampolineScheduler.java:52)
at io.reactivex.internal.operators.single.SingleSubscribeOn.subscribeActual(SingleSubscribeOn.java:37)
at io.reactivex.Single.subscribe(Single.java:3096)
at io.reactivex.internal.operators.single.SingleObserveOn.subscribeActual(SingleObserveOn.java:35)
at io.reactivex.Single.subscribe(Single.java:3096)
at io.reactivex.Single.subscribeWith(Single.java:3140)
at com.example.zach.weatherapp.viewModel.CityListViewModel.getCities(CityListViewModel.kt:27)
at com.example.zach.weatherapp.viewModel.CityListViewModelTest.getCities(CityListViewModelTest.kt:58)
答案 0 :(得分:0)
此行被调用后:
// CityListViewModelTest
val result = viewModel.getCities(0.0,0.0)
CityListViewModel
将订阅forecastRepo.getCities()
,因此verify(repository).getCities(0.0,0.0)
通过是有意义的。
但是,由于forecastRepo.getCache()
在单独的线程上运行,因此不能保证verify(repository).getCache()
会在forecastRepo.getCities()
之前被调用。在测试代码中,您需要使用TestSchedulers等待io Scheduler中的操作完成。
旁注:
由于ViewModels独立于Android生命周期,因此ViewModels中的.observeOn(AndroidSchedulers.mainThread())
在这里似乎没有多大作用。除了使用setValue()
之外,您还可以使用postValue()
从后台线程更新MutableLiveData
。
更新:
尝试使用此:
@Rule
@JvmField
val rule = InstantTaskExecutorRule()
@Mock
lateinit var observer: Observer<List<City>>
@Test
fun getCities() {
val response = getMockedCities(5)
`when`(repository.getCities(ArgumentMatchers.anyDouble(), ArgumentMatchers.anyDouble()))
.thenReturn(Single.just(response))
viewModel.getCities(0.0,0.0).observeForever(observer)
verify(repository).getCities(0.0,0.0)
verify(repository).getCache()
// assertEquals(response.list,result.value) //result.value should be a list of 5 mocked cities but is null
verify(observer).onChanged(reponse.list)
}
也可能是getCache()
和getCache().saveCities()
的问题。如果上述代码不起作用,请尝试对它们进行模拟。
答案 1 :(得分:0)
通过手工创建虚拟响应解决了问题。 getMockedCities()返回带有空变量(如预期)的City对象的数组,但我的City对象的变量不可为空。另外我还必须模拟repository.getCache()