Testing Fragment in isolation with FragmentFactory

Quan Vu
AndroidPub
Published in
3 min readNov 25, 2019
Source: http://www.businessofapps.com/

Modular Android application has been adopted by a lot of engineers these days. Multi module architecture has brought a lot of benefits including:

  • Faster build time
  • Faster time to run your UnitTest suites
  • Features can be developed independently
  • And many more…

Being able to UI test a fragment from a module without building the whole application could save engineers a lot of time. Today I want to share an approach to test your Fragment in isolation in a multi module project.

Sample application

I have created a simple application that allows user to manage and schedule reminders. Below is the multi module structure of the application.

In this article, we will try to write test for fragment inside “Details Feature” module. Do note that all the following code will be placed in the same module

Instantiate your fragment under test

To instantiate a fragment under the test, there are two tasks we need to be done

  • Provide all the dependencies that the fragment requires
  • Provide a host activity to launch that fragment

Let’s try to solve both of them one by one.

1. Provide all the dependencies that the fragment requires

To provide all the dependencies for a fragment, we can put all of its dependencies into a dependency interface.

Big thanks to FragmentFactory we can now inject dependencies to the Fragment constructor.

Now you can use any dependency injection libraries to initialise other objects (like Presenter, ViewModels, …). Here is an example using Dagger.

From now we can create a new instance of our fragment and provide any mock dependencies we want through its constructor.

2. Provide a host activity to launch that fragment

Thanks to FragmentScenario provided by google, we can solve this problem easily.

  • It creates an empty activity to launch our fragment under test.
  • We can also pass our mock version of FragmentFactory to FragmentScenario

Provide mock dependencies

Lets start with a simple mock version of FragmentFactory

With the help of the dependency interface, we are free to provide any kind of mocks as long as we satisfy the interface. In this example, I provide an in-memory database instead of a SQLite one.

Source: https://giphy.com/

Time to write our lovely test

From this point writing a test should be straightforward. The following test will

  • Launch a fragment and create a new reminder
  • Verify that the reminder is created in database
  • Verify that an alarm is scheduled

Conclusion

Thanks to the help of FragmentFactory and FragmentScenario we can now test Fragment in isolation easily. But every engineer solution has its own pros and cons.

Pros

  • Testing fragment in isolation save us a lot of time in multi module project.
  • By instantiating the fragment in isolation, we can launch and interact with our fragment manually without building the whole application.
  • Multiple teams can work independently without stepping on each other.

Cons

  • Satisfying the dependency interface could take a lot of time. To reduce this pain, we could create and reuse mock objects across our multi module project.
  • This approach will not work with retain fragment. If your application is still using retain fragment, this approach will leak the dependency interface. On another hand, Google itself has deprecated retain fragment.

Get the code

The full implementation of the application could be found here

Thank you for reading ❤ ❤ ❤

--

--