Android Livedata存储库单元测试问题

时间:2019-07-10 14:24:41

标签: android unit-testing mvvm repository android-livedata

我正在尝试为存储库中的数据库获取方法编写单元测试。

public class CategoriesRepository {

private final CategoryDao categoryDao;
private final AppExecutors mAppExecutors;
private MutableLiveData<List<Category>> categoryLivedata;


 public LiveData<List<Category>> getCategories() {

  categoryLivedata.setValue(null);
  categoryLivedata = (MutableLiveData<List<Category>>) 
  categoryDao.getAllCategory();
  return categoryLivedata;
 }


} 

当我尝试为上述方法编写单元测试时,

public class CategoriesRepositoryTest {

    private CategoryDao categoryDao = mock(CategoryDao.class);
    private CategoriesRepository repo = CategoriesRepository.getInstance(categoryDao, new InstantAppExecutors());

    @Rule
    public InstantTaskExecutorRule instantExecutorRule = new InstantTaskExecutorRule();

    @Test
    public void getCategoriesTest(){
        MutableLiveData<List<Category>> dbData = new MutableLiveData<List<Category>>();
        List<Category> categoryTest = TestUtils.createCategoryList();
        dbData.setValue(categoryTest);
        when(categoryDao.getAllCategory()).thenReturn(dbData);

        Observer<List<Category>> observer = mock(Observer.class);


        LiveData<List<Category>> categories = repo.getCategories();
        categories.observeForever(observer);  // line no. 45
        verify(observer).onChanged(categoryTest);

    }

}

它返回nullpointerexception:

java.lang.NullPointerException
    at com.cybage.roomapplication.category.CategoriesRepositoryTest.getCategoriesTest(CategoriesRepositoryTest.java:45)
    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.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    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)

请帮助我解决问题。

0 个答案:

没有答案