Search code examples
javaandroidunit-testingandroid-livedataandroid-viewmodel

Cannot setValue to a MutableLiveData while doing Unit Test - throwing java.lang.reflect.InvocationTargetException


I have a User class that has a builder and the constructor for this class is private.

public class User {

    private String name;

    private User() {
    }

    public static class UserBuilder {
        private User user;

        public UserBuilder() {
            user = new User();
        }

        public UserBuilder withName(String name) {
            user.name = name;
            return this;
        }

        public User build() {
            return user;
        }
    }
}

And then I want to have a ViewModel class where I want to have a MutableLiveData for User class to be observed from my activity/fragment.

public class UserViewModel extends ViewModel {

    private User.UserBuilder userBuilder;
    private MutableLiveData<User> user;

    public UserViewModel() {
        userBuilder = new User.UserBuilder();
        user = new MutableLiveData<>(userBuilder.build());
    }

    public void setName(String name) {
        userBuilder.withName(name);
        user.setValue(userBuilder.build());
    }

    public MutableLiveData<User> getUser() {
        user.setValue(userBuilder.build());
        return user;
    }
}

In the above implementation, I found that it is throwing a NullPointerException followed by an InvocationTargetException while calling the setValue function from my unit test.

I searched for the reason and looks like, the User class needs to have a public constructor for this to work with MutableLiveData. However, this is not the case, I tried making a public constructor and it still fails.

I am trying to write some unit test and I am getting the following error in the logcat. Please note that I am using the following to address the mainThread error I suppose.

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();

Here is what I am trying to do in the unit test.

@RunWith(MockitoJUnitRunner.class)
public class UserViewModelTest {

    private UserViewModel userViewModel;

    @Mock
    private Observer<User> userObserver;

    @Rule
    public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();

    @Before
    public void setup() {
        initMocks(this);
        userViewModel = new userViewModel();
        userViewModel.getUser().observeForever(userObserver);
    }
}

And the exception is in the following line.

user.setValue(userBuilder.build());

Here is the logcat.

java.lang.NullPointerException
    at androidx.arch.core.executor.DefaultTaskExecutor.isMainThread(DefaultTaskExecutor.java:77)
    at androidx.arch.core.executor.ArchTaskExecutor.isMainThread(ArchTaskExecutor.java:116)
    at androidx.lifecycle.LiveData.assertMainThread(LiveData.java:460)
    at androidx.lifecycle.LiveData.setValue(LiveData.java:304)
    at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
    at com.example.viewmodel.UserViewModel.getUser(UserViewModel.java:79)
    at com.example.viewmodels.UserViewModelTest.setup(UserViewModelTest.java:66)
    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.RunBefores.evaluate(RunBefores.java:24)
    at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
    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.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:74)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:80)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    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)

Thanks in advance!


Solution

  • Finally, I could understand what's causing the exception. Looks like when the Observer is set to a ViewModel it takes some time to get initialized and in the meantime, if we are trying to setValue to a LiveData, it throws the NullPointerException as the ViewModel is not initialized yet. This is a problem with the test environment setup as @CommonsWare suggested in his comments earlier in this question. I really appreciate that as it led me to find a way of getting rid of the exception.

    I decided to write the unit test in Kotlin, as it was easier to have a lateinit variable. I found a very good tutorial here. I am posting the code associated to test the ViewModel here.

    Create a helper class named MockUtils.kt first.

    import org.mockito.Mockito
    
    /**
     * Helper function to mock classes with types (generics)
     */
    inline fun <reified T> mock(): T = Mockito.mock(T::class.java)
    

    And finally, the test class should look like the following.

    class UserViewModelTest {
    
        @get:Rule
        val rule = InstantTaskExecutorRule()
    
        private lateinit var viewModel: UserViewModel
    
        private val observer: Observer<User> = mock()
    
        @Before
        fun before() {
            viewModel = UserViewModel()
            viewModel.user.observeForever(observer)
        }
    
        @Test
        fun testUserViewModel() {
            val expectedUser = viewModel.user.value
            viewModel.setName("John")
    
            val captor = ArgumentCaptor.forClass(User::class.java)
            captor.run {
                verify(observer, times(2)).onChanged(capture())
                assertNotNull(expectedUser)
                assertEquals("John", value.name)
            }
        }
    }
    

    In the way of getting rid of the exception, I had to fix another problem. I thought it would be helpful for other developers if I mention that here.

    I made a mistake of using different versions of InstantTaskExecutorRule and LiveData. You can check the tweet from Jeroen Mols here. I had the same problem. The LiveData was imported from the androidx package whereas the InstantTaskExecutorRule was imported using the android package - had to fix that as follows.

    implementation "androidx.lifecycle:lifecycle-viewmodel:2.1.0"
    implementation "androidx.lifecycle:lifecycle-livedata:2.1.0"
    implementation "androidx.lifecycle:lifecycle-extensions:2.1.0"
    testImplementation "androidx.arch.core:core-testing:2.1.0"
    

    I created a Github repository with simple classes from this question so that anyone can check and run the unit tests using their own development environment. Happy coding!