gpt4 book ai didi

android - 即使在添加 RxImmediateSchedulerRule 之后,android.os.Looper 中的方法 getMainLooper 仍然没有被模拟

转载 作者:行者123 更新时间:2023-12-04 23:36:23 29 4
gpt4 key购买 nike

趋势 View 模型测试

@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()
}

//...
}

趋势 View 模型
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 调用,它还 Hook 到“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$1.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$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.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取决于您的测试功能。

关于android - 即使在添加 RxImmediateSchedulerRule 之后,android.os.Looper 中的方法 getMainLooper 仍然没有被模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58057769/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com