gpt4 book ai didi

android - 在 android.os.Looper 中使用 LiveData 方法 getMainLooper 进行单元测试未模拟

转载 作者:行者123 更新时间:2023-12-03 17:30:47 28 4
gpt4 key购买 nike

在尝试进行单元测试时,我无法让 liveData.postValue 正常工作。我一直在谷歌中寻找解决方案,这是我现在拥有的代码。

public class ProjectListViewModelTest {

GetProjectList getProjectList = Mockito.mock(GetProjectList.class);
ProjectModel.Project project = new ProjectModel.Project("testing",
"this is a test",
"https://logo.jpg",
new ProjectModel.Company("cat"),
"20150404",
"active");
List<ProjectModel.Project> projects = Arrays.asList(project);
ProjectModel.ProjectList projectsList = new ProjectModel.ProjectList(projects);

ProjectsListViewModel projectsListViewModel;

private PublishSubject<ProjectModel.ProjectList> projectsListPublishSubject = PublishSubject.create();

@Rule public InstantTaskExecutorRule instantExecutorRule = new InstantTaskExecutorRule();

@BeforeClass
public static void setUpRxSchedulers() {
Scheduler immediate = new Scheduler() {
@Override
public Disposable scheduleDirect(@NonNull Runnable run, long delay, @NonNull TimeUnit unit) {
return super.scheduleDirect(run, 0, unit);
}

@Override
public Scheduler.Worker createWorker() {
return new ExecutorScheduler.ExecutorWorker(Runnable::run);
}
};

RxJavaPlugins.setInitIoSchedulerHandler(scheduler -> immediate);
RxJavaPlugins.setInitComputationSchedulerHandler(scheduler -> immediate);
RxJavaPlugins.setInitNewThreadSchedulerHandler(scheduler -> immediate);
RxJavaPlugins.setInitSingleSchedulerHandler(scheduler -> immediate);
RxAndroidPlugins.setInitMainThreadSchedulerHandler(scheduler -> immediate);
}

@Before
@Throws(exceptionClasses = Exception.class)
public void setUp() {
MockitoAnnotations.initMocks(this);
projectsListViewModel = new ProjectsListViewModel(getProjectList);
when(getProjectList.execute()).thenReturn(projectsListPublishSubject.take(1).singleOrError());
}

@Test
public void testExecuteGetProjectsListSuccess() {
LiveData<List<ProjectModel.MapProject>> liveData = projectsListViewModel.getLiveData();
ProjectModel.MapProject expectedResult = new ProjectModel.MapProject(
"testing", "this is a test", "https://logo.jpg",
"cat", "2015-04-04", "active");
projectsListViewModel.getProjects();
projectsListPublishSubject.onNext(projectsList);
Assert.assertEquals(expectedResult, liveData.getValue().get(0));
}

@After
public void tearDownClass(){
RxAndroidPlugins.reset();
}

我在 setUpRxSchedulers 中的代码是强制性的,以避免与 Rx 出现相同的错误 ( Method getMainLooper in android.os.Looper not mocked)。但是我无法解决调用 liveData.post(projectList) 时遇到的这个错误。在我查看的所有论坛中,他们都说 @Rule public InstantTaskExecutorRule instantExecutorRule = new InstantTaskExecutorRule();问题应该得到解决。但不是我的情况。

我也将 View 模型放在这里以防万一:
public class ProjectsListViewModel extends ViewModel {

GetProjectList getProjectList;
MutableLiveData<List<ProjectModel.MapProject>> liveData = new MutableLiveData<>();

public ProjectsListViewModel(GetProjectList getProjectList){
this.getProjectList = getProjectList;
}

public LiveData<List<ProjectModel.MapProject>> getLiveData(){
return liveData;
}

public void getProjects(){
getProjectList.execute()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(ProjectModel.ProjectList::getProjects)
.toObservable().flatMapIterable(projects -> projects)
.map(project -> project.convertToMapProject()).toList()
.subscribe(projectsList ->
liveData.setValue(projectsList));
}

}

最佳答案

InstantTaskExecutorRule的用法实际上会解决这个问题。
我认为问题在于在 JUnit 5 中 @Rule似乎不再支持注释( as Extensions are now the way to go )。代码将成功编译,但不会应用规则。
(至少)有两种解决方案:
使用 JUnit 4
肯定是更快,可能不是最好的,这取决于您需要多少 JUnit 5。
这可以通过更改 setup 的注释来完成。来自 @BeforeEach 的方法至@Before并通过导入 @Test来自 JUnit 4 的注释。
这是您的导入应该是什么样子。

import org.junit.Before
import org.junit.Rule
import org.junit.Test
实现 InstantTaskExecutorExtension如果您关心使用 JUnit 5,这会更好:)
Here's an article讲的是如何精确实现 InstantTaskExecutorExtension .
完成后记得使用 @ExtendWith(InstantTaskExecutorExtension::class) 将它应用到您的测试类中。注释而不是 @Rule !

关于android - 在 android.os.Looper 中使用 LiveData 方法 getMainLooper 进行单元测试未模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54574641/

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