- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我尝试在我的应用程序上测试一些方法,但在调用 mutablelivedata.postValue 时出现错误。这是一个 fragment 和错误消息:
@Test
public void callStartScreenRepository(){
Observer<User> userObserver = mock(Observer.class);
startScreenViewModel.returnUser().observeForever(userObserver);
maennlich = new User();
maennlich.setVorname("Christian");
MutableLiveData<User> userTest = new MutableLiveData<>();
userTest.postValue(maennlich);
when(startScreenRepository.userWebService("testUser")).thenReturn(userTest);
verify(userObserver).onChanged(maennlich);
}
java.lang.RuntimeException:未模拟 android.os.Looper 中的方法 getMainLooper。参见 http://g.co/androidstudio/not-mocked了解详情。
at android.os.Looper.getMainLooper(Looper.java)
at android.arch.core.executor.DefaultTaskExecutor.postToMainThread(DefaultTaskExecutor.java:48)
at android.arch.core.executor.AppToolkitTaskExecutor.postToMainThread(AppToolkitTaskExecutor.java:100)
at android.arch.lifecycle.LiveData.postValue(LiveData.java:277)
at android.arch.lifecycle.MutableLiveData.postValue(MutableLiveData.java:28)
at com.example.cfrindt.foodtracking.ViewModels.StartScreenViewModelTest.callStartScreenRepository(StartScreenViewModelTest.java:102)
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:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
无论我使用 setValue() 还是 postValue(),我都会收到此错误。我怎样才能让它发挥作用?
最佳答案
首先,您需要使用 InstantTaskExecutorRule,这意味着:
@Rule public InstantTaskExecutorRule instantExecutorRule = new InstantTaskExecutorRule();
这将允许立即使用 MutableLiveData。请记住在您的应用程序的 build.gradle 中包含导入:
testCompile "android.arch.core:core-testing:$rootProject.ext.arch_version"
然后,您需要使用覆盖 RxSchedulers 的规则定义 JUnit 规则,或者在 @Before 中使用 RxAndroidPlugins 覆盖调度程序(如果您使用的是 RxJava)
RxSchedulersOverrideRule:
public class RxSchedulersOverrideRule implements TestRule {
@Override public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override public void evaluate() throws Throwable {
RxAndroidPlugins.reset();
RxAndroidPlugins.setMainThreadSchedulerHandler(scheduler -> Schedulers.trampoline());
RxJavaPlugins.reset();
RxJavaPlugins.setIoSchedulerHandler(scheduler -> Schedulers.trampoline());
RxJavaPlugins.setComputationSchedulerHandler(scheduler -> Schedulers.trampoline());
RxJavaPlugins.setNewThreadSchedulerHandler(scheduler -> Schedulers.trampoline());
base.evaluate();
RxAndroidPlugins.reset();
RxJavaPlugins.reset();
}
};
}
}
并在测试类中调用它:
@Rule public RxSchedulersOverrideRule rxSchedulersOverrideRule = new RxSchedulersOverrideRule();
另一个选项是在 @Before 的 setup() 中调用:
RxAndroidPlugins.setInitMainThreadSchedulerHandler(schedulerCallable -> Schedulers.trampoline());
使用后需要在@After 的 tearDownClass() 中重置:
RxAndroidPlugins.reset();
关于android - UnitTest 中 MutableLiveData 的 setValue 和 postValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45988310/
我如何转换 MutableLiveData至 MutableLiveData . val text = NonNullMutableLiveData("") 我的类 NonNullMutableLi
private val _users = MutableLiveData>() val users: LiveData> get() = _users fun getUsers() { vie
我在 ViewModel 中有一个函数“A”,它从 firebase 检索数据,并将该值分配给 MutableLiveData (所有这些都包含在 onSuccessListener 中)并返回。此函
我有带有编辑文本和 2 个按钮的简单 View ,我希望用户能够使用按钮将数量增加/减少 1,或者他可以在编辑文本小部件中手动写入数量,但这个值不能小于 0 并且大于10. 我正在使用带有 LiveD
我正在使用 MutableLiveData 开发一个 Activity ,以使我的 fragment 与从搜索检索到的结果保持同步,但在更新结果列表后不会触发观察者。 我在主要 Activity 上有
我对 Viewmodel 中的 MutableLiveData 有疑问。 MutableLiveData 的 setValue 功能是否触发观察?如果我们更改 MutableLiveData 的内容而
我在应用的架构组件中使用 LiveData 和 ViewModel。 我有一个分页的项目列表,当用户向下滚动时我加载更多。查询的结果存储在一个 MutableLiveData> 当我执行初始加载并将变
如何使用初始值初始化 MutableLiveData?我正在寻找类似的东西: val text = MutableLiveData("initial value") 最佳答案 MutableLiveD
我在尝试更新我的 MutableLiveData 时遇到问题。我正在从我的 ViewModel 调用登录函数来更新我的 UI。在我的 ViewModel 中,我调用了我的 API 服务器,但是当我调用
我在 MVVM 架构上使用多个 MutableLiveData。 在 ViewModel 上,我发布了对象,但 fragment 没有恢复。 当 fragment 恢复时,观察者得到 MutableL
class Foo : ViewModel() { val bars: MutableLiveData> = MutableLiveData() get() { if(f
如何将新项目添加到 MutableLiveData 列表?我想构建一个无限滚动的 Recyclerview。所以我有一个包含 10 个项目的列表,在单击“加载更多按钮”后,我想添加 10 个新项目。
我在我的应用程序中使用实时数据,并且我有一个从 ViewModel 扩展的 View 模型. 在我的 View 模型中,我有一个列表: var songs: MutableLiveData> = Mu
我有 MyViewModel类变量 MutableLiveData items哪些商店,列表Item类(class)。还有功能fetchData()通过 Retrofit 库和 checkStatus
我有两个非常相似的函数,我试图通过使用泛型来避免代码中的重复。这些函数都有一个 try catch block ,并使用两种不同类型的两个 MutableLiveData 通知其观察者: val no
我观察到 MutableLiveData 会触发观察者的 onChanged,即使向其 setValue 方法提供相同的对象实例也是如此。 //Fragment#onCreateView - scen
更新: 如果我移动到另一个 fragment 并返回到这个 fragment ,则 TextView 会更新...... 我无法通过使用 setValue() 或 postValue() 让 UI 中
我发现了新的 Android 架构组件,我想通过一个小型测试应用程序来测试 ViewModel/LiveData 。后者有两个 fragment (在 ViewPager 中),第一个创建/更新卡片列
我现在陷入困境,目前想知道为什么我的可变数组列表即使使用postvalue()更新也返回null。我尝试使用Toast显示它,并且显示[],我认为它为null。它之间没有空间,因此看起来像一个盒子。我
在 Kotlin 中,声明变量类型时,使用冒号。即使在声明 LiveData 时,也会使用冒号。那么为什么 等号 用于 MutableLiveData?我一直无法弄清楚这一点。几天前我花了大约 2 个
我是一名优秀的程序员,十分优秀!