Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing I can't add Observer to LiveData NullPointerException

Why i get nullPointerException despite if i remove observer and use mLivedata.getValue() and assertNotNull(mLiveData) it works well?

@RunWith(PowerMockRunner.class)
public class LoginRepositoryTest {

   //Data
   LoginRepository loginRepository;


   @Rule
   InstantTaskExecutorRule rule = new InstantTaskExecutorRule();


   @Mock
   AppExecutors appExecutors;
   @Mock
   LoginService loginService;
   @Mock
   Observer<Resource<Session>> loginObserver;
   @Mock
   ApiResponse<Session> loginResponse;
   @Mock
   ArgumentCaptor<Resource<Session>> resultLDCaptor;
   @Before
   public void setUp(){
       MockitoAnnotations.initMocks(this);
       RxAndroidPlugins.setInitMainThreadSchedulerHandler(scheduler -> Schedulers.trampoline());
       loginRepository=spy(new LoginRepository(appExecutors,loginService));
   }

   @Test
   public void testLoginSuccess(){


       //Prepare Login service
       MutableLiveData<ApiResponse<Session>> serviceLD=spy(new MutableLiveData<>());
       when(loginService.login(mock(RequestBody.class))).thenReturn(serviceLD);

       //Perform Login
       loginRepository.login(mock(RequestBody.class)).observeForever(loginObserver);

       //capture any value
       verify(loginObserver).onChanged(resultLDCaptor.capture());

       //Now value must be Resource.Loading
       assertNotNull(resultLDCaptor.getValue());
       assertTrue(resultLDCaptor.getValue().isLoading());

       //Now return fake data from server
       when(loginResponse.isSuccessful()).thenReturn(true);
       loginResponse.data=mock(Session.class);
       serviceLD.postValue(loginResponse);

   }

And the error

 
java.lang.NullPointerException
    at android.arch.lifecycle.MediatorLiveData$Source.plug(MediatorLiveData.java:141)
    at android.arch.lifecycle.MediatorLiveData.onActive(MediatorLiveData.java:118)
    at android.arch.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:410)
    at android.arch.lifecycle.LiveData.observeForever(LiveData.java:207)
    at com.wimoapp.android.login.LoginRepositoryTest.testLoginSuccess(LoginRepositoryTest.java:74)
    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.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:202)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:144)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    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)
 
like image 670
Sayed Shalan Avatar asked Oct 16 '22 11:10

Sayed Shalan


1 Answers

Make sure that whatever live data is being observed by the MediatorLiveData is not null:

myMediatorLiveData.addSource(repository.getLiveData(), ...);
                                        ^^^^^^^^^^^

In my case, repository.getLiveData() returned null because repository is mocked. In your test you can easily return a live data instance by using mockito, for example:

MutableLiveData<...> liveData = new MutableLiveData<>();    
when(repository.getLiveData()).thenReturn(liveData);
like image 104
Markus Avatar answered Oct 19 '22 02:10

Markus