0

我正在尝试对视图模型中的可观察对象进行单元测试,但出现以下错误。我已经包括了有问题的可观察对象...

getRepository()
                .executeGetAccount()
                .doOnSubscribe(response -> liveData.postValue(Resource.loading()))
                .subscribeOn(getSchedulerProvider().io())
                .observeOn(getSchedulerProvider().ui())

这是我的测试课

@Config(sdk = {Build.VERSION_CODES.O_MR1})
@RunWith(RobolectricTestRunner.class)
@PowerMockIgnore({ "org.robolectric.*", "android.*", "javax.net.ssl.*", "androidx.*"  })
public class DashboardActivityViewModelTest {

    @Rule
    public PowerMockRule powerMockRule = new PowerMockRule();

    private DashboardActivityViewModel viewModel;
    String result = "";

    AppSchedulerProvider sProvider;

    AppPreferencesHelper appPreferencesHelper;

    @Before
    public void init(){
        sProvider = PowerMockito.mock(AppSchedulerProvider.class);
        appPreferencesHelper = PowerMockito.mock(AppPreferencesHelper.class);
        RetrofitTestHelper retrofitTestHelper = new RetrofitTestHelper(appPreferencesHelper, sProvider);

        viewModel = new DashboardActivityViewModel(retrofitTestHelper.provideRepository(), sProvider);

    }


    @Test
    public void testRepositoryInit(){
        assertNotNull(sProvider);
        assertNotNull(appPreferencesHelper);
        assertNotNull(viewModel);
    }

    @Test
    public void testGetAccount(){
        viewModel.executeGetAccount();
        assertNotNull(viewModel.getAccountsWithBalanceLiveData().getValue());
    }



}

这里是错误日志:

java.lang.NullPointerException: scheduler is null

    at io.reactivex.internal.functions.ObjectHelper.requireNonNull(ObjectHelper.java:39)
    at io.reactivex.Observable.subscribeOn(Observable.java:12343)
    at com.example.xxxx.xxx.dashboard.DashboardActivityViewModel.executeGetAccount(DashboardActivityViewModel.java:384)
    at com.example.xxxx.xxx.DashboardActivityViewModelTest.testGetAccount(DashboardActivityViewModelTest.java:88)
    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.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:83)
    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.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1819)
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:801)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:666)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
    at org.powermock.classloading.AbstractClassloaderExecutor.getResult(AbstractClassloaderExecutor.java:69)
    at org.powermock.classloading.AbstractClassloaderExecutor.executeWithClassLoader(AbstractClassloaderExecutor.java:59)
    at org.powermock.classloading.SingleClassloaderExecutor.execute(SingleClassloaderExecutor.java:67)
    at org.powermock.classloading.AbstractClassloaderExecutor.execute(AbstractClassloaderExecutor.java:43)
    at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:75)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:546)
    at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:252)
    at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
4

0 回答 0