android.os.Looper中使用LiveData方法getMainLooper进行单元测试

时间:2019-02-07 13:39:18

标签: android unit-testing junit android-looper android-livedata

在尝试进行单元测试时,无法使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();应该可以解决问题。但是不是我的情况。

我也将视图模型放在这里,以防它可以帮助您

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));
    }

}

1 个答案:

答案 0 :(得分:0)

使用InstantTaskExecutorRule实际上可以解决这个问题。

我认为问题在于,在JUnit 5中似乎不再支持@Rule注释(as Extensions are now the way to go)。该代码将成功编译,但不会应用该规则。

(至少)有两种解决方法:

使用JUnit 4

肯定更快,可能不是最好的,具体取决于您需要多少JUnit 5。

只需将setup方法的注释从@BeforeEach更改为@Before并从JUnit 4导入@Test注释即可完成。 这就是您的导入外观。

import org.junit.Before
import org.junit.Rule
import org.junit.Test

实施InstantTaskExecutorExtension

如果您关心使用JUnit 5,这会更好:)

Here's an article讨论如何精确实现InstantTaskExecutorExtension。 完成后,请记住使用@ExtendWith(InstantTaskExecutorExtension::class)注释而不是@Rule将其应用于您的测试类!