Migrate a Robolectric test to FragmentScenario

Ricardo Belchior
Mar 28, 2019 · 2 min read
Image for post
Image for post

This blog post describes my experiences migrating a unit test from Robolectric ‘old’ API to the new AndroidX FragmentScenario API. I wanted to do that as part of experimenting this new API, which brings some advantages over the old method (see http://robolectric.org/androidx_test/).

My previous approach usually involved setting up the parent activity and then simulating all the steps until the Fragment under test was visible in that activity — which was not trivial, because it involved setting up the test dependencies for both the Fragment and parent activity.

As an example:

Methods performQuery, progressBar and shouldBeVisible, use the standard View methods to assert stuff — mostly View#findViewById and View#getVisibility

Moving on, the new FragmentScenario API from AndroidX allows developers to test a given Fragment in isolation of the parent activity. Plus, it provides methods to drive the Fragment’s lifecycle states for testing.

As a curiosity, under-the-hood this API will also declare and setup an ‘EmptyActivity’ and inject the Fragment into it. That’s why the documentation mentions the dependency should be included as debugImplementation (typically we would use testImplementation for test dependencies).

One consequence of testing the Fragment in isolation is that you can no longer have hard dependencies to the parent activity. For example, I usually casted my Activity at Fragment#onAttach:

// On the Fragmentoverride fun onAttach(context: Context) {
if (context is ParentActivity) {
parentActivity = context
} else {
throw Exception("$context must implement ParentActivity")

override fun onDetach() {
parentActivity = null

Where now a recommended approach is to set the ParentActivity through your Activity#onAttachFragment callback:

// On the Activityoverride fun onAttachFragment(@NonNull fragment: Fragment) {
if (fragment is SearchFragment) {

Moving on, in order to test the fragment you have to launch it using one of the available launch* methods and assert stuff using either the #onFragment callback or using Espresso view matchers.

Here’s the same test migrated to the FragmentScenario API:

According to the documentation, calling FragmentScenario#onFragment synchronously waits for the application to be idle and provides a reference to the Fragment that makes it easy to make assertions on your views.

Note: If you have custom views, you might want to specify a theme when setting up the FragmentScenario, otherwise it will complain that it cannot find a given resource.

And that’s it! Even though the fragment-testing library is still in alpha, I did not find any issues along the way. It was easy to use and seems to provide a safer way of running tests. I will most likely begin to use it next time I write a new test.

I hope you found this useful, let me know your thoughts =)

And feel free to follow me on Twitter https://twitter.com/belchii

Reach Product Development

The product development and engineering teams behind Reach…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store