Skip to content

Instantly share code, notes, and snippets.

@pbochenski
Last active April 10, 2018 18:16
Show Gist options
  • Save pbochenski/e9aef99158003f70f273cf0e2b9d43b0 to your computer and use it in GitHub Desktop.
Save pbochenski/e9aef99158003f70f273cf0e2b9d43b0 to your computer and use it in GitHub Desktop.
testing LiveData

java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. See http://g.co/androidstudio/not-mocked for details.
at android.os.Looper.getMainLooper(Looper.java)
at android.arch.core.executor.DefaultTaskExecutor.isMainThread(DefaultTaskExecutor.java:58)
at android.arch.core.executor.AppToolkitTaskExecutor.isMainThread(AppToolkitTaskExecutor.java:115)
at android.arch.lifecycle.LiveData.assertMainThread(LiveData.java:408)
at android.arch.lifecycle.LiveData.setValue(LiveData.java:290)
at android.arch.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
at pl.pbochenski.archcomponentstest.MainViewModelTest.shouldGetPostsAndHaveACopyOfIt(MainViewModelTest.kt:48) ...

testCompile("android.arch.core:core-testing:$archVersion", {
exclude group: 'com.android.support', module: 'support-compat'
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.android.support', module: 'support-core-utils'
})
class MainViewModelTest {
@Rule
@JvmField
val rule = InstantTaskExecutorRule()
@Test
fun someTest(){
}
}
@Test
fun shouldGetPostsAfterUpdate() {
//given
val returnedItem = createDummyItem()
val observer = mock<Observer<List<Post>>>()
model.getPosts().observeForever(observer)
//when
liveData.value = listOf(returnedItem)
//than
verify(observer).onChanged(listOf(Post(returnedItem.id, returnedItem.title, returnedItem.url)))
}
@vilius-kraujutis-ht
Copy link

vilius-kraujutis-ht commented Mar 21, 2018

@pbochenski, it should be then instead of than.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment