我无法在尝试进行单元测试时使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(项目列表(时遇到的此错误。在我检查的所有论坛中,他们都说应该用@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));
}
}
InstantTaskExecutorRule
的使用实际上可以解决这个问题。
我认为问题是在 JUnit 5 中似乎不再支持@Rule
注释(因为扩展现在是要走的路(。代码将成功编译,但不会应用规则。
对此(至少(有两种解决方案:
使用 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 :),这会更好
这是一篇讨论如何精确实现的文章 InstantTaskExecutorExtension
.完成后,请记住使用@ExtendWith(InstantTaskExecutorExtension::class)
注释而不是@Rule
将其应用于您的测试类!