android.os.Looper 中的方法 getMainLooper 即使在添加 RxImmediateSchedulerRule 后仍未被模拟

Method getMainLooper in android.os.Looper not mocked still occuring even after adding RxImmediateSchedulerRule

TrendingViewModelTest

@RunWith(JUnit4::class)
class TrendingViewModelTest {
    private lateinit var trendingRepository: TrendingRepository
    private lateinit var trendingViewModel: TrendingViewModel

    @get:Rule
    val schedulers = RxImmediateSchedulerRule()

    @Before
    fun setUp() {
        trendingRepository = mock(TrendingRepository::class.java)
        trendingViewModel = TrendingViewModel(trendingRepository)
    }

    @Test
    fun testWithNetwork() {
        trendingViewModel.isConnected = true
        trendingViewModel.fetchTrendingRepos()
        verify(trendingRepository, times(1)).getTrendingRepos()
    }

    //...
}

TrendingViewModel

fun fetchTrendingRepos() {
if (isConnected) {
    loadingProgress.value = true
    compositeDisposable.add(
        trendingRepository.getTrendingRepos().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ response ->
                run {
                    loadingProgress.value = false
            },
                { error ->
                    loadingProgress.value = false
                }
            )
    )
} 

RxImmediateSchedulerRule:

class RxImmediateSchedulerRule : TestRule {
    override fun apply(base: Statement?, description: Description?): Statement {
        return object : Statement() {
            @Throws(Throwable::class)
            override fun evaluate() {
                RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
                RxJavaPlugins.setComputationSchedulerHandler { Schedulers.trampoline() }
                RxJavaPlugins.setNewThreadSchedulerHandler { Schedulers.trampoline() }
                RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }

                try {
                    base?.evaluate()
                } finally {
                    RxJavaPlugins.reset()
                    RxAndroidPlugins.reset()
                }
            }
        }
    }
}

TrendingRepositoryImpl:

class TrendingRepositoryImpl @Inject constructor(
    val apiService: GitHubApi,
    val trendingDao: AppDao
) : TrendingRepository {

    override fun getTrendingRepos(): Single<List<TrendingRepo>> {
        return apiService.getTrendingGits()
    }
}

趋势存储库:

interface TrendingRepository {
    fun getTrendingRepos(): Single<List<TrendingRepo>>
}

fetchTrendingRepos() 内部启动了一个 Rxjava 调用,它还挂钩到 'AndroidSchedulers.mainThread()',这可能是导致它的原因。

java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. at android.os.Looper.getMainLooper(Looper.java) at androidx.arch.core.executor.DefaultTaskExecutor.isMainThread(DefaultTaskExecutor.java:77) at androidx.arch.core.executor.ArchTaskExecutor.isMainThread(ArchTaskExecutor.java:116) at androidx.lifecycle.LiveData.assertMainThread(LiveData.java:461) at androidx.lifecycle.LiveData.setValue(LiveData.java:304) at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50) at com.manoj.trendgitz.mvvm.ui.TrendingViewModel.fetchTrendingRepos(TrendingViewModel.kt:32) at com.manoj.trendgitz.TrendingViewModelTest.testWithNetwork(TrendingViewModelTest.kt:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access[=17=]0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

当您更新 LiveData 值时,您还应该添加 @get:Rule var rule: TestRule = InstantTaskExecutorRule()

不要忘记在 build.gradle 文件中添加以下内容:

dependencies {
    // ...
    testImplementation "androidx.arch.core:core-testing:2.1.0"
}

此外,相应地更改测试代码以避免 NullPointerException:

@Test
fun testWithNetwork() {
    trendingViewModel.isConnected = true
    Mockito.`when`(trendingRepository.fetchTrendingRepos()).thenReturn(Single.just(listOf<TrendingRepo>()))
    trendingViewModel.fetchTrendingRepos()
    verify(trendingRepository, times(1)).getTrendingRepos()
}

Mockito.when() 允许您在每次调用模拟方法时执行不同的操作。如果你不使用它,你可能会看到可能 NullPointerException 取决于你的测试功能。

在您应用的 build.gradle

中的 android {} 下添加以下行
testOptions {
            // Used for Unit testing Android dependent elements in /test folder
            unitTests.includeAndroidResources  = true
            unitTests.returnDefaultValues = true
        }

如果您在没有添加 RxImmediateSchedulerRule 的情况下看到此错误。尝试将此规则添加到您的测试 class:

@Rule @JvmField
val instantTaskExecutorRule = InstantTaskExecutorRule()