Skip to main content

Changes in Robolectric 4.2 and Issues Preventing Update

This article was translated from Japanese by Claude Code.

f🆔shaunkawano:20190312231526p:plain

What is Robolectric?
#

Robolectric is an open-source unit test framework for Android. Without needing to launch an emulator or real device, you can run tests in a pseudo-Android environment within the JVM, allowing you to use and test Android platform classes like Activity in tests.

github.com

Robolectric 4.2
#

About a month ago, version 4.2 was released as the latest version.

※For details, please see the robolectric release page.

A list of deprecated APIs as of this release is here:

robolectric.org

Making Behavior Consistent with Device Execution
#

Starting with Robolectric 4.2, changes have been made to unify the behavior when running Robolectric tests with the behavior when running tests on a device.

Specifically, when writing tests for launching an Activity using Robolectric or using Context, tests were previously written like this:

val scenario = ActivityScenario.launch(FragmentActivity::class.java)
...

scenario.onActivity {
  ... // assert some text with activity as `it`
}

From this version onward, such a way of specifying Activity launch in Robolectric tests results in an NPE crash. And this is intentional behavior from Robolectric.

What this means is that previously when specifying Activity launch like the above, Robolectric would implicitly and automatically create an AndroidManifest entry and control the test to run. However, this was unique behavior to Robolectric and differed from behavior on actual devices, so starting with version 4.2, it no longer automatically generates such behaviors or implements unique controls. Instead, it basically conforms to Android Framework behavior.

The trend toward unifying execution on devices and in virtual environments makes sense to me.

So going forward, when running Activity-based tests with Robolectric, you need to define the Activities you use in tests in your actual AndroidManifest.xml and reference them during tests.

Unresolved Issues
#

However, at the time of writing, there are still issues preventing an update to the latest version. The issue is that the current Android Gradle Plugin doesn’t merge AndroidManifest.xml files specified in testImplementation modules (or perhaps something Robolectric can solve?).

https://issuetracker.google.com/issues/127986458

As a workaround, the Robolectric repository’s issue introduces that if you define a test-specific module (e.g., testsupport module) within your project, you can address this temporarily by implementation-ing that module on the app/build.gradle side, while doing implementation project(':app') in testsupport/build.gradle.

github.com

However, I tried this workaround straightforwardly by creating a sample repository, but I wasn’t able to solve it successfully, so I’m currently stuck unable to update.

For those who want to see specifically how this is implemented, please see below:

github.com

I’m quite likely making mistakes or misunderstandings in my approach, so if you notice anything, I’d appreciate any guidance via comments 🙏

When I succeed in updating, I plan to update this article by adding to it.

That’s all—it’s just a report about being stuck and unable to update.