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)?
Related
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.
I'm trying to run a unit test on my RecyclerView. For my first test, I want to see if the RecyclerView is displayed.
#RunWith(RobolectricTestRunner::class)
class WordListFragmentTest {
// Executes task sin the Architecture component in the same thread.
#get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
private lateinit var scenario: FragmentScenario<WordListFragment>
private lateinit var viewModel: MainViewModel
val word = Word("Word")
#Before
fun setup() {
viewModel = mock(MainViewModel::class.java)
scenario = launchFragment(
factory = MainFragmentFactory(viewModel),
fragmentArgs = null,
themeResId = R.style.Theme_Words,
initialState = Lifecycle.State.RESUMED
)
}
#Test
fun `recyclerView displayed`() {
onView(withId(R.id.recyclerView))
.check(matches(isDisplayed()))
}
After running the test I get the following error.
java.lang.Exception: Main looper has queued unexecuted runnables. This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.
This appears to be related to LiveData observer that submits the list in the fragment. If I comment out the submit function the test will run.
The Fragment.
class WordListFragment(private val viewModel: MainViewModel) : Fragment() {
...
private fun submitList() {
viewModel.wordList.observe(viewLifecycleOwner, {
it?.let {
rvAdapter.submitList(it)
}
})
}
}
MianViewModel
class MainViewModel #Inject constructor(
var repository: IWordRepository,
#IoDispatcher var ioDispatcher: CoroutineDispatcher
) : ViewModel() {
var wordList: LiveData<List<Word>> = repository.allWords
...
}
This link states Robolectric will default to LooperMode.LEGACY behavior, but this can be overridden by applying a #LooperMode(NewMode) annotation to a test package, test class, or test method, or via the 'robolectric.looperMode' system property. I'm still experiencing the same error when I run my test.
I am new to testing and I am writing a test for my ViewModel. I have a couple of questions about it.
In my Test method, unless I add a delay(10), the test fails saying:
Wanted but not invoked:
breweryRepository.getBeerStyles();
-> at com.helenc.test.repositories.BreweryRepository.getBeerStyles(BreweryRepository.kt:12)
Actually, there were zero interactions with this mock.
Why do I need a delay? Is there a better way to do it?
I added the test in the test folder (not in androidTest). Is this correct? I am not sure since I am using some androidx dependencies, like androidx.lifecycle.Observer or androidx.arch.core.executor.testing.InstantTaskExecutorRule.
This is my ViewModel test file:
#RunWith(JUnit4::class)
class BeerStylesViewModelTest {
private lateinit var viewModel: BeerStylesViewModel
private lateinit var repository: BreweryRepository
private lateinit var beerStylesObserver: Observer<Resource<List<StylesData>>>
private val successResource = Resource.success(mockedBeerStylesList)
#Rule
#JvmField
val instantExecutorRule = InstantTaskExecutorRule()
#ObsoleteCoroutinesApi
private val mainThreadSurrogate = newSingleThreadContext("UI thread")
#ObsoleteCoroutinesApi
#ExperimentalCoroutinesApi
#Before
fun setUp() {
Dispatchers.setMain(mainThreadSurrogate)
repository = mock()
runBlocking {
whenever(repository.getBeerStyles()).thenReturn(successResource)
}
viewModel = BeerStylesViewModel(repository)
beerStylesObserver = mock()
}
#Test
fun `when load beerStyles success`() = runBlocking {
viewModel.beerStyles.observeForever(beerStylesObserver)
delay(10)
verify(repository).getBeerStyles()
verify(beerStylesObserver).onChanged(Resource.loading(null))
verify(beerStylesObserver).onChanged(successResource)
}
#ObsoleteCoroutinesApi
#ExperimentalCoroutinesApi
#After
fun tearDown() {
Dispatchers.resetMain()
mainThreadSurrogate.close()
}
}
I clearly don't understand how to unit test business logic inside Transformation. In my specific case I need to test Transformations.map, but I guess Transformations.switchmap would be the same.
The following is just an example of my scenario, and what I'd like to achieve.
MyViewModel.kt
class MyViewModel: ViewModel() {
private val _sampleLiveDataIwannaTest : MutableLiveData<Int> = MutableLiveData()
val sampleLiveDataIWannaTest: Livedata<Int> = _sampleLiveDataIWannaTest
// ...
val liveDataImNotInterestedIn = Transformations.map(myRepository.streamingData){
streaming->
_sampleLiveDataIwannaTest.postValue(streaming.firstElementValue +streaming.lastElementValue)
streaming
}
// ...
}
With:
val liveDataImNotInteresedIn : LiveData<Foo>
myRepository.streamingData : LiveData<Foo>
myRepository.streamingData is a data source that wakes up the Transformations.map which, in turn, starts the business logic I'm interested in (the value posted in _sampleLiveDataIwannaTest). In this particular test, I don't care about anything else.
MyViewModelTest.kt
class MyViewModelTest {
#get:Rule val rule = InstantTaskExecutorRule()
#RelaxedMockK
lateinit var myRepository : MyRepository
#OverrideMockKs
lateinit var sut: MyViewModel
#Before
fun setUp() {
MockKAnnotations.init(this, relaxUnitFun = true)
}
#Test
fun Transformations_Test(){
sut.liveDataImNotInterestedIn.observeForever{}
// 1)I really don't know how to mock the livedata that returns from
// myRepository.streamingData . Something like this is correct?
// every{myRepository.streamingData}.returns{< LiveData of type Int > }
// 2) I wish to write this kind of test:
//
// assertEquals(5, sampleLiveDataIWannaTest.value)
}
I'm using MockK instead of Mockito.
The unit test code will look like this:
class MyViewModelTest {
#get:Rule
val rule = InstantTaskExecutorRule()
#RelaxedMockK
lateinit var myRepository : MyRepository
#RelaxedMockK
lateinit var mockedSampleLiveDataIWannaTest : Observer<Int>
#OverrideMockKs
lateinit var sut: MyViewModel
#Before
fun setUp() {
MockKAnnotations.init(this, relaxUnitFun = true)
}
#Test
fun Transformations_Test(){
val expected = (*YOUR EXPECTED DATA HERE FROM REPOSITORY*)
every { myRepository.streamingData() } answers { expected }
sut.sampleLiveDataIWannaTest.observeForever(mockedSampleLiveDataIWannaTest)
verify { myRepository.streamingData() }
verify() { mockedSampleLiveDataIWannaTest.onChanged(Int) }
confirmVerified(myRepository, mockedSampleLiveDataIWannaTest)
}
if your repository is using coroutines then change every to coEvery and verify to coVerify
to learn more about MockK: https://mockk.io/
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)