Robolectric can't find resource or manifest file

Solution to the problem described here: http://blog.futurice.com/android_unit_testing_in_ides_and_ci_environments

The Missing Manifest

You should have noticed by now that Robolectric complains about not being able to find your Android Manifest. We’ll fix that by writing a custom test runner. Add the following as app/src/test/java/com/example/app/test/RobolectricGradleTestRunner.java:

package com.example.app.test;

import org.junit.runners.model.InitializationError;
import org.robolectric.manifest.AndroidManifest;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.res.Fs;

public class RobolectricGradleTestRunner extends RobolectricTestRunner {
  public RobolectricGradleTestRunner(Class<?> testClass) throws InitializationError {
    super(testClass);
  }

  @Override
  protected AndroidManifest getAppManifest(Config config) {
    String myAppPath = RobolectricGradleTestRunner.class.getProtectionDomain()
                                                        .getCodeSource()
                                                        .getLocation()
                                                        .getPath();
    String manifestPath = myAppPath + "../../../src/main/AndroidManifest.xml";
    String resPath = myAppPath + "../../../src/main/res";
    String assetPath = myAppPath + "../../../src/main/assets";
    return createAppManifest(Fs.fileFromPath(manifestPath), Fs.fileFromPath(resPath), Fs.fileFromPath(assetPath));
  }
}

Remember to change the RunWith annotation in the test class.


I have faced same errors, We used several flavors and buildtypes So, there are steps to make it working:

1) Android studio tests run configuration

You have to set working directory to $MODULE_DIR$ in Windows too. http://robolectric.org/getting-started/ should say that.

2) Unit test should be annotated like this:

@RunWith(RobolectricTestRunner.class) 
@Config(constants = BuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml", packageName = "com.example.yourproject") 
public class SomeFragmentTest {

Here we have plain link to manifest file and packageName


From the 4.0 version the use of @Config is not highly appreciated, however the updated Getting started guide solve my problem: http://robolectric.org/getting-started/

Only one build.gradle modification, and the Manifest file loaded, the tests can run.

android {
  testOptions {
    unitTests {
      includeAndroidResources = true
    }
  }
}

The related issue on github (also has been closed): https://github.com/robolectric/robolectric/issues/3328