java.util.NoSuchElementException: Flow is empty - android

I'm trying to test this repository using a unit test with JUnit 4
class AppRepository #Inject constructor(
private val networkHelper: NetworkHelper,
private val weatherMapper: WeatherMapper,
private val cacheHelper: CacheHelper
) : Repository {
override fun fetchWeatherInfo(lat: Double, lng: Double): Flow<AppResult<Weather>> {
return networkHelper.getWeatherInfo(lat, lng).map {
when(it){
is AppResult.Error -> AppResult.Error(it.errorMessage)
is AppResult.Success -> AppResult.Success(weatherMapper.map(it.data))
}
}
}
}
So I've made a unit test class like this using coroutine dispatcher and mock some interfaces
for repository
#ExperimentalCoroutinesApi
#RunWith(JUnit4::class)
class AppRepositoryTest{
private val dispatcher = TestCoroutineDispatcher()
#Mock
private lateinit var networkHelper: NetworkHelper
#Mock
private lateinit var cacheHelper: CacheHelper
private val mapper=WeatherMapper()
private lateinit var repository: AppRepository
#Before
fun setup(){
MockitoAnnotations.openMocks(this)
repository= AppRepository(networkHelper,mapper,cacheHelper)
}
#Test
fun`check if weather info returned failed`(){
dispatcher.runBlockingTest {
val expected:AppResult<Weather> = AppResult.Error("Can't load weather info")
`when`(networkHelper.getWeatherInfo(1.0,1.0)).thenReturn(
flow { AppResult.Error("Can't load weather info") }
)
val result= repository.fetchWeatherInfo(1.0,1.0).single()
assertEquals(expected,result)
}
}
}
I want to test a failed case for the current repository function but the failed crashed due to this error
Flow is empty
java.util.NoSuchElementException: Flow is empty
at kotlinx.coroutines.flow.FlowKt__ReduceKt.single(Reduce.kt:62)
at kotlinx.coroutines.flow.FlowKt.single(Unknown Source)
at com.isma3il.photoweatherapp.data.repositories.AppRepositoryTest$check if weather info returned failed$1.invokeSuspend(AppRepositoryTest.kt:73)
at com.isma3il.photoweatherapp.data.repositories.AppRepositoryTest$check if weather info returned failed$1.invoke(AppRepositoryTest.kt)
at com.isma3il.photoweatherapp.data.repositories.AppRepositoryTest$check if weather info returned failed$1.invoke(AppRepositoryTest.kt)
so how to fix this issue and how to use flow in testing?

I totally forget to emit data inside the flow builder
like this
`when`(networkHelper.getWeatherInfo(1.0,1.0)).thenReturn(
flow { emit(AppResult.Error("Can't load weather info")) }
)
and that is works for me.

Related

What is the best way to test a function in my viewModel?

I started to implement automated tests recently and I have a little doubt of how to test my ViewModel functions. My project follows some clean architecture concepts, I'm using lib kotlin for mockito (https://github.com/nhaarman/mockito-kotlin) and I would like to do the following test: When call makeLoginUser get success then set loginUserLiveData .
I have studied some concepts and I know the tests I should do, but I am still in doubt as to how to do them, which objects should I mock for a given test and which should be instantiated. I needed a small example to address me.
Here is my test class and some classes of the structure of my project.
TestClass
#RunWith(JUnit4::class)
class MainViewModelTest {
#get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
#Mock
private lateinit var myRepository: MyRepository
private lateinit var loginUserUseCase: LoginUserUseCase
private lateinit var checkUserAuthenticatedUsecase: CheckUserAuthenticatedUsecase
private lateinit var logoutUserUseCase: LogoutUserUseCase
private lateinit var mainViewModel: MainViewModel
#Before
fun setUp() {
MockitoAnnotations.initMocks(this)
}
#Test
fun `When call makeLoginUser get success then set loginUserLiveData`() {
// prepare
myRepository = mock { // should I mock or instantiate?
on { execLoginUser(ArgumentMatchers.anyString(), ArgumentMatchers.anyString()) } doReturn Flowable.just(true)
}
loginUserUseCase = mock { // should I mock or instantiate?
on { execute(ArgumentMatchers.anyMap()) } doReturn Flowable.just(true)
}
mainViewModel = MainViewModel(loginUserUseCase, checkUserAuthenticatedUsecase, logoutUserUseCase)
val observer = mock<Observer<ResultState<Boolean>>> { mock }
// Execute
mainViewModel.makeLoginUser("test#gmail.com", "123456")
// Check
// ?
}
}
ViewModel
// components 'LoginUserUseCase', 'CheckUserAuthenticatedUsecase' and 'LogoutUserUseCase' injected via koin
class MainViewModel(
private val loginUserUseCase: LoginUserUseCase,
private val checkUserAuthenticatedUsecase: CheckUserAuthenticatedUsecase,
private val logoutUserUsecase: LogoutUserUseCase
): BaseViewModel() {
val loginUserLiveData = MutableLiveData<ResultState<Boolean>>()
val userAuthenticatedLiveData = MutableLiveData<ResultState<Boolean>>()
val logoutUserLiveData = MutableLiveData<ResultState<Boolean>>()
fun makeLoginUser(email: String, password: String) {
loginUserLiveData.postValue(ResultState.Loading())
loginUserUseCase.execute(mapOf(EMAIL to email, PASSWORD to password))
.subscribe({
loginUserLiveData.postValue(ResultState.Success(it))
}, {
loginUserLiveData.postValue(ResultState.Error(it))
}).addTo(disposables)
}
...
}
UseCase Domain Class
// components 'ThreadExecutor' and 'PostExecutionThread' injected via koin
abstract class FlowableUseCase<T, in Params> constructor(
private val threadExecutor: ThreadExecutor,
private val postExecutionThread: PostExecutionThread) {
protected abstract fun buildUseCaseObservable(params: Params? = null): Flowable<T>
open fun execute(params: Params? = null): Flowable<T> {
return this.buildUseCaseObservable(params)
.subscribeOn(Schedulers.from(threadExecutor))
.observeOn(postExecutionThread.scheduler)
}
}
Abstraction UseCases
// components 'MyRepository', 'ThreadExecutor' and 'PostExecutionThread' injected via koin
// 'MyRepository' abstracts remote and local data repository
class LoginUserUseCase(
private val myRepository: MyRepository,
threadExecutor: ThreadExecutor,
postExecutionThread: PostExecutionThread
): FlowableUseCase<Boolean, Map<String, Any>?>(threadExecutor, postExecutionThread) {
override fun buildUseCaseObservable(params: Map<String, Any>?) = myRepository
.execLoginUser(
params?.get(EMAIL) as String,
params[PASSWORD] as String
)
}

Mockito does'nt mock repository

i'm try to test my ViewModel with mockito.
This is my TestClass:
#RunWith(JUnit4::class)
class RatesViewModelTest {
#Rule #JvmField
open val instantExecutorRule = InstantTaskExecutorRule()
#Mock
var observer: Observer<Pair<ArrayList<CurrencyExchangerModel>,Boolean>>? = null
#Mock
private lateinit var repository: RatesRepository
private var currencyList = ArrayList<CurrencyModel>()
private lateinit var viewModel : RatesViewModel
#Before
fun setUp(){
MockitoAnnotations.initMocks(this)
currencyList.add(CurrencyModel("BASE"))
viewModel = RatesViewModel(repository!!)
viewModel.getCurrencyExchangerObservableList().observeForever(observer!!)
}
#Test
fun testNull(){
Mockito.`when`(repository.getFlowableRates()).thenReturn( Flowable.just(currencyList) )
assertTrue(viewModel.getCurrencyExchangerObservableList().hasObservers())
}
}
When this method is invoked:
Mockito.`when`(repository.getFlowableRates()).thenReturn( Flowable.just(currencyList) )
I got this error:
kotlin.UninitializedPropertyAccessException: lateinit property db has
not been initialized
Here the repository:
open class RatesRepository(context:Context) : BaseRepository(context){
#Inject
lateinit var ratesAPI: RatesAPI
#Inject
lateinit var db: Database
/**
* load the updated chatList from API
*/
fun loadCurrencyRatesFromAPI(): Single<ArrayList<CurrencyModel>> {
val supportedCurrency = context.resources.getStringArray(R.array.currencies)
return ratesAPI.getLatestRates(EUR_CURRENCY_ID).map { RatesConverter.getRatesListFromDTO(it,supportedCurrency) }
}
/**
* save rates on DB
*/
fun saveCurrencyRatesOnDB(list:ArrayList<CurrencyModel>): Completable {
return db.currencyRatesDAO().insertAll(list)
}
/**
* get flawable rates from DB
*/
fun getFlowableRates(): Flowable<List<CurrencyModel>> {
return db.currencyRatesDAO().getAll()
}
companion object {
const val EUR_CURRENCY_ID = "EUR" //BASE
}
}
What i'm doing wrong ?
Thx !
When you define behaviour of a mock and use the when(...).then(...) notation of mockito,
the method itself is invoked (by mockito, normally not relevant for your test).
In your case that is a problem because db is not initialized.
To avoid this issues use the doReturn(...).when(...) syntax in these cases,
which does not cause the method invocation.
Mockito.doReturn(Flowable.just(currencyList)).when(repository).getFlowableRates();
(You might need to adjust this java syntax to make it kotlin compatible)

Why onChange result is "false" in test LiveData

It's first my post on stackoverflow and i'm beginer in kotlin, Lifecycle, need help with it. I lost 2 days with it and need help.
I have SplashViewModel class
class SplashViewModel #Inject constructor(
private val configuration: IConfiguration,
private val compositeDisposable: CompositeDisposable) : BaseViewModel(compositeDisposable), SplashContract.ViewModel{
override val isLoggedLiveData: MutableLiveData<Boolean> = MutableLiveData()
init {
setLoginStatus()
}
override fun setLoginStatus(){
isLoggedLiveData.postValue(configuration.isUserLoggedIn())
}}
SplashViewModelTest class
class SplashViewModelTest : BaseTest(){
#get:Rule
val testRule = InstantTaskExecutorRule()
#Mock
private lateinit var configuration: IConfiguration
#Mock
private lateinit var compositeDisposable: CompositeDisposable
#Mock
private lateinit var observer: Observer<Boolean>
private lateinit var viewModel: SplashContract.ViewModel
override fun setup() {
super.setup()
trampolineRxPlugin()
viewModel = SplashViewModel(
configuration,
compositeDisposable
)
}
override fun tearDown() {
super.tearDown()
verifyNoMoreInteractions(
configuration,
compositeDisposable
)
}
#Test
fun `should change livedata status to true when viewmodel is initialize`() {
val isLogged = true
`when`(configuration.isUserLoggedIn()).thenReturn(isLogged)
viewModel.isLoggedLiveData.observeForever(observer)
verify(configuration, Mockito.times(1)).isUserLoggedIn()
verify(observer).onChanged(isLogged)
}
When run this test result is error
Argument(s) are different! Wanted:
observer.onChanged(true);
-> at com.example.kotlinmvvm.feature.splash.viewModel.SplashViewModelTest.should check configuration user login status when getIsLoggedLiveData is called(SplashViewModelTest.kt:85)
Actual invocation has different arguments:
observer.onChanged(false);
-> at androidx.lifecycle.LiveData.considerNotify(LiveData.java:113)
Comparison Failure:
Expected :observer.onChanged(true);
Actual :observer.onChanged(false);
Who knows what's going on?
What I suspect is happening is that you're instantiating view model (in setup) prior to following being invoked (with isLogged = true)...which is causing code in init to be invoked...and at that point it will return false.
`when`(configuration.isUserLoggedIn()).thenReturn(isLogged)
Did you intend perhaps to explicitly call setLoginStatus in your test as well (after above line)?

Android Unit Testing with Mockito for External REST API calls

I am new to JUnit test cases in Android. I heard that by using Mockito we can achieve easily.
My Android Class makes an external call to a REST API Service (Retrofit) that returns a JSON response. I have to mock that response (hardcoded JSON) and write test cases.
Please share your idea how to achieve this.
i am able to achieve.
updating with a piece of code.
class GenerateTripViewModelTest {
#Mock
private lateinit var mockRepository: GenerateTripRepository
private val schedulerProvider = SchedulerProvider(Schedulers.trampoline(), Schedulers.trampoline())
private lateinit var generateTripViewModel: GenerateTripViewModel
#Before
fun setUp() {
MockitoAnnotations.initMocks(this)
generateTripViewModel = GenerateTripViewModel(mockRepository, schedulerProvider)
}
#Test
fun SearchSuccessCase() {
val tripReq = GenerateTripReqModel(ArrayList<String>(),"123","xxxx","xxxx")
Mockito.`when`(mockRepository.generateTrip(tripReq)).thenReturn(Observable.just(GenerateTripResModel("")))
val testObserver = TestObserver<GenerateTripResModel>()
generateTripViewModel.generateTrip(tripReq).subscribe(testObserver)
testObserver.assertNoErrors()
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
class GenerateTripViewModel #Inject constructor(private val generateTripRepository: GenerateTripRepository,private val schedulerProvider: SchedulerProvider) : ViewModel() {
fun generateTrip(reqModel: GenerateTripReqModel) = generateTripRepository.generateTrip(reqModel)
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#Singleton
class GenerateTripRepository #Inject constructor(
private val apiServices: ApiServices, private val context: Context,
private val appExecutors: AppExecutors = AppExecutors()) {
/**
* Generate Trip
*/
fun generateTrip(reqModel: GenerateTripReqModel): Observable<GenerateTripResModel> = apiServices.generateTrip(reqModel)
}
class Test{
#Mock
lateinit var redditApiService: RedditApiService
lateinit var postSettingsViewModel: PostSettingsViewModel
#Before
fun setUp() {
initMocks(this)
postSettingsViewModel = PostSettingsViewModel(redditApiService, userRepo)
}
#Test
fun testApi(){
Mockito.`when`(redditApiService.getSubreddits("asd")).thenReturn(Single
.just<SubredditResponse>(SubredditResponse(listOf(Subreddit("first")))))
//make your tests
}
}
You can use viewmodel or presenter and pass your api service there. With mockito you can specify function call and its return value. Mockito.when(api.get()).thenReturn(new Result()).
You can check google samples: https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/test/java/com/android/example/github/api/GithubServiceTest.kt
You can use product flavors for mock your response or user MockWebServer library or replace your retrofit interface with you implementation what return json from assets
upd: I misunderstood the question. I agree with Phowner Biring

Mockito wanted but not invoked - However, there was exactly 1 interaction with this mock

This is the first time I use mockito & roboelectric to make unit test on android studio.
I want to verify that view.onSuccessLogin() called when request to server / API is success. But, I got an error "wanted but not invoked - However, there was exactly 1 interaction with this mock"
Here is my presenter class :
class PresenterLogin internal constructor(private val view: View) {
private val compositeDisposable = CompositeDisposable()
interface View : BaseView {
fun onSuccessLogin()
}
fun requestLogin(username: String, password: String) {
view.showProgressDialog()
val disposable = ApiHelper.service.requestSignIn(ApiHeaders().headers,
username, password, AppPreference.getInstance().firebaseToken)
.map { response -> return#map response.toString() }
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(
{ response ->
view.hideProgressDialog()
view.onSuccessLogin()
},
{ error ->
view.hideProgressDialog()
view.showError(error.message)
}
)
compositeDisposable.add(disposable)
}
fun onViewDestroyed() {
compositeDisposable.dispose()
}
}
This is my PresenterLoginTest class
#RunWith(RobolectricTestRunner::class)
#Config(constants = BuildConfig::class)
class PresenterLoginTest {
private lateinit var activity: ActivityLogin
#Mock
lateinit var view: PresenterLogin.View
#Mock
lateinit var service: ApiService
#Mock
lateinit var callLogin: Observable<Login>
private lateinit var presenter: PresenterLogin
#Before
fun setUp() {
activity = Robolectric.buildActivity(ActivityLogin::class.java)
.create()
.resume()
.get()
MockitoAnnotations.initMocks(this)
presenter = PresenterLogin(view)
}
#Test
#Throws(Exception::class)
fun shouldNotBeNull() {
assertNotNull(activity) // Success
}
#Test
fun requestLogin() {
Mockito.`when`(service.requestSignIn(
ApiHeaders().headers, "081212345678", "12345", ""))
.thenReturn(Observable.just(Login()))
presenter.requestLogin("081212345678", "12345")
verify(view).showProgressDialog() // Success
verify(view).onSuccessLogin()
/*
Wanted but not invoked:
view.onSuccessLogin();
-> at package.PresenterLoginTest.requestLogin(PresenterLoginTest.kt:117)
However, there was exactly 1 interaction with this mock:
view.showProgressDialog();
*/
}
}
showProgressDialog method is called before mocked method so it passed verify (this is 1 interaction with this mock),
If you mock method requestSignIn then it won't send a normal request which produce a response that subscribe will atached to, and therefore also its nested method onSuccessLogin won't be executed.
I solve the problem by following this repo :
https://github.com/alfianyusufabdullah/RetrofitUnitTesting/tree/master/retrofit
The concept is to use RepositoryCallback so we can mock the response from the API

Categories

Resources