vendredi 11 novembre 2016

Robolectric: Test-only Activity with AppCompat Theme

I currently have a library project that defines an abstract activity class that extends AppCompatActivity and provides some nontrivial functionality that needs to be tested.

I would like to avoid instrumentation tests since all other tests in the project are written in Robolectric. However, I can't seem to figure out how to properly test this activity using Robolectric.

The problem is that the Activity is not in the manifest, so I can't specify the theme, hence all tests fail with the "You need to use a Theme.AppCompat theme (or descendant) with this activity." error.

I first tried to create a custom manifest and load it using the @Config(manifest = ...), but the manifest couldn't be found. So I tried to move the activity to the main source set and added it to the real manifest, hoping that Robolectric will find it there. Nope, still getting "No such manifest file: build/intermediates/manifests/full/debug/AndroidManifest.xml". Actually, this path does not seem to change, so I suspect that the config option is not working at all.

So, to sum up:

  • How do you properly use @Config(manifest = ...) with library projects?
  • How do you test an Activity with custom theme that isn't declared in the main source set? (assuming the manifest option doesn't work)

For the sake of completeness:

  • I have the @RunWith(RobolectricTestRunner::class) properly set
  • All other tests seem to work fine, except for this one.
  • Fails both in AndroidStudio and in pure Gradle build
  • Robolectric 3.1.4
  • Gradle Android plugin 2.2.2
  • Gradle 3.1
  • I tried playing with this custom runner quite a bit, but no luck - it actually managed to change the manifest path, but then Robolectric fails on some internal null pointer exception...

Aucun commentaire:

Enregistrer un commentaire