Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Testing in Android Unit Testing - Now Featuring Robolectric! Testing with Robolectric

Kevin Eaton
Kevin Eaton
9,527 Points

Robolectric Fails and Manifest

When trying to test with Robolectric in the video, I run the test and get the following:

WARNING: No manifest file found at ./AndroidManifest.xml.Falling back to the Android OS resources only. To remove this warning, annotate your test class with @Config(manifest=Config.NONE).

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

When I specify the manifest file directly, such as:

@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP, manifest = "src/main/AndroidManifest.xml")

I get:

java.lang.NullPointerException at org.robolectric.shadows.ShadowAssetManager$OverlayedStyle.equals(ShadowAssetManager.java:336) at org.robolectric.shadows.ShadowAssetManager.applyThemeStyle(ShadowAssetManager.java:309) at org.robolectric.shadows.ShadowResources$ShadowTheme.applyStyle(ShadowResources.java:246) at android.content.res.Resources$Theme.applyStyle(Resources.java) at android.view.ContextThemeWrapper.onApplyThemeResource(ContextThemeWrapper.java:128) at android.app.Activity.onApplyThemeResource(Activity.java:3661) at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:140) at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:85) at android.support.v7.app.AppCompatActivity.setTheme(AppCompatActivity.java:86) at org.robolectric.shadows.ShadowActivity.setThemeFromManifest(ShadowActivity.java:94) ....

My gradle file includes the following:

testCompile 'org.robolectric:robolectric:3.1'

There are a few things you could try; The first one is checking your JUnit Run Configurations and set the Working Directory to $MODULE_DIR$

Since it's been two years, there are newer versions of the framework Roboletric and also Android Studio. Therefore, for anyone else running into similar issues, I would recommend having a look at the robolectric getting started page. http://robolectric.org/getting-started/