#StackBounty: #android #unit-testing #kotlin #mockito #android-architecture-components Android Architecture Components GithubBrowserSam…

Bounty: 100

Hi folks I’m trying to learn unit tests with Kotlin using Google’s github browser sample as a base. My code is very similar indeed but I can’t get one of their more basic tests to work (and I don’t really understand it).

My main question is what preciesly is the sendResultToUI() test doing,

public void sendResultToUI() {
    MutableLiveData<Resource<User>> foo = new MutableLiveData<>();
    Observer<Resource<User>> observer = mock(Observer.class);
    verify(observer, never()).onChanged(any(Resource.class));
    User fooUser = TestUtil.createUser("foo");
    Resource<User> fooValue = Resource.success(fooUser);


from what I can tell, it says:

  • a) When loadUser("foo") is called, instead of executing the function
    just return a new live data guy called foo.
  • b) Observe the userViewModel.getUser() live data
  • c) Call setLogin("foo"), which triggers the live data and calls loadUser("foo")
  • d) Verify the observer to getUser() we created earlier has never been fired by any instance of a Resource
  • e) Create a successful foo user, verify that setting it’s value triggers the getUser() observer

So if all of that is roughly correct, my question is on step d). My code is throwing an exception:

java.lang.IllegalStateException: ArgumentMatchers.any(T::class.java) must not be null

So I guess onChanged is called with a null value. I really don’t get what the hell is going on here – calling setLogin() in step c) triggers the user switchMap live data which in turn calls userRepositiory.loadUser(), so that should call the observer to getUser() yet we ask to verify the opposite (that it’s never called). After all calling loadUser() returns foo which we specified in a). Maybe if someone can at least explain the test to me I might make sense of my own code!

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.