'How make a API unit test with MVVM architecture in Android
I have a project that uses Retrofit with MVVM architecture, but unfortunately I don't have much experience with unit testing, so I can't do a test on MVVM, I'm using Koin for dependency injection, I also had to mask some things in the code, unfortunately. Follow my code:
SomethingRepository
interface SomethingRepository {
@GET("something/")
suspend fun getSomethingList(@Query("page") page: Int): List<Something>
}
SomethingRepositoryData
class SomethingRepositoryData( private val repository: SomethingRepository ) {
suspend fun somethingList(page: Int = 1): List<Something> {
return repository.getSomethingList(page)
}
}
SomethingViewModel
class SomethingViewModel(
private val SomethingRepositoryData: SomethingRepositoryData
) : ViewModel() {
private val _somethingList = MutableLiveData<List<Something>>()
val somethingList: LiveData<List<Something>>
get() = _somethingList
fun getSomethingList() {
CoroutineScope(Dispatchers.IO).launch {
try {
somethingRepositoryData.somethingList().run {
_somethingList.postValue(this)
}
} catch (e: Exception) {
// Do something
}
}
}
}
SomethingTest ???
I dont have ideal, i see in the internet is necessary uses Mockito and create a new RepositoryData just for Mock, I dont understand very well how make a API unit test
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
