45

While trying to get Robolectric RC3 to work in Android Studio, I get

Caused by: java.lang.RuntimeException: build/intermediates/bundles/debug/AndroidManifest.xml not found or not a file; it should point to your project's AndroidManifest.xml
at org.robolectric.manifest.AndroidManifest.validate(AndroidManifest.java:120)
at org.robolectric.manifest.AndroidManifest.getResourcePath(AndroidManifest.java:469)
at org.robolectric.manifest.AndroidManifest.getIncludedResourcePaths(AndroidManifest.java:475)
at org.robolectric.RobolectricTestRunner.createAppResourceLoader(RobolectricTestRunner.java:479)
at org.robolectric.RobolectricTestRunner.getAppResourceLoader(RobolectricTestRunner.java:471)
at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:73)
at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:421)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234)

I tried

@Config(manifest = "app/src/main/AndroidManifest.xml", constants = BuildConfig.class, sdk=21)

and I tried setting the manifest location in my TestRunner, nothing worked. In the file system I see that the manifest is in

./app/build/intermediates/manifests/full/debug/AndroidManifest.xml

not in the location Robolectric is looking for it. At one point the manifest just got ignored, then a similar issue occurred for resources, the app or Robolectric couldn't find a raw resource my app uses. This is in my build file:

   sourceSets {
    main {
        java.srcDirs = ['src/main/java']
        resources.srcDirs = ['src/main/res']
    }
    test {
        java.srcDirs = ['src/test/java', 'src/main/java']
        resources.srcDirs = ['src/test/res', 'src/main/res']
    }
}

How do I tell Robolectric where to look for a manifest, and more importantly, where to look for resources?

Edit: I have checked out Robolectric from github, I've built it, installed it in my local .m2 repo, the gradle file now refers to the local SNAPSHOT build, and I made sure Gradle doesn't get a new version from a remote repo. Then I copied the RobolectricGradleTestRunner to my project, I have changed the lines where the file locations are defined: it didn't contain the module name. Now it works.

Christine
  • 5,617
  • 4
  • 38
  • 61

9 Answers9

73

I'm assuming you're trying to run the tests with JUnit. You can try two different things:

  1. Create a Custom TestRunner class, as shown here. Check the CustomTestRunner section, where you basically create a TestRunner that actually knows the right manifest to use. Specify your tests for them to run with your test runner, with the @Config annotation.
  2. (My preferred choice) Go the your JUnit configuration, Run > Edit Configurations. Notice the 'Working Directory' textbox. Append /app (for OSX and Linux) or \app (Windows), to the path written in the textbox. Try running again and it should work.
J.C. Chaparro
  • 1,079
  • 10
  • 13
66

Same problem on Android Studio. I've solved this problem to edit the configuration of Unit4. you can follow these things.

On Android Studio.

  1. Edit Configurations
  2. In Junit, you have to change the working directory to $MODULE_DIR$.

The important thing is $MODULE_DIR$.

you can reference the following screenshot. thanks.

enter image description here

enter image description here

Rooney
  • 1,195
  • 10
  • 18
  • 2
    This is the correct answer. It is an issue that affects Mac users. This is actually documented on the Robolectric Getting Started page, http://robolectric.org/getting-started/. – Matt Accola Nov 12 '15 at 14:40
  • I'm new in Robolectric; just setup the project and probably did something wrong. Still, this answer resolved my issue on Windows. – pepan Feb 14 '16 at 22:31
  • But why AS need it to every new test ? Is it any way to Indicate "Working directory" for all tests? – Vladyslav Ulianytskyi Jul 04 '17 at 14:11
  • I don't know why based on other comments it should work but I can not get it working on AS 3.5. Anyone else facing the same? – HBB20 Sep 02 '19 at 00:08
9

In case you are still getting this error with Android Studio 3.0, please be sure that your gradle configuration has these parameters:

android {
    ...
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}
busradeniz
  • 317
  • 3
  • 6
  • Many thanks! That saved my day! I was really struggling with new gradle plugin 3.0.0, gradle 4.1 and Android studio 3. My Robolectric tests went nuts! Now everything is back to normal. – Nunes D. Nov 13 '17 at 19:29
  • Thank you. This saved me possibly hours of debugging. – rickchristie Feb 23 '21 at 07:22
6

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.

  1. 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 {

Kirill Vashilo
  • 1,559
  • 1
  • 18
  • 27
  • 3
    Make sure you are importing the correct BuildConfig - I wasn't paying attention and pulled BuildConfig from a library which causes a similar issue to the OP. – carlrice Dec 16 '15 at 21:44
2

Note that intellij 16 EAP has a bug around this $MODULE_DIR$ variable (it's pointing to the wrong place), causing the tests to fail with this exception. See https://youtrack.jetbrains.com/issue/IDEA-149802#tab=History. Should get fixed mid Jan 2016.

sakis kaliakoudas
  • 2,039
  • 4
  • 31
  • 51
2

Please find the below steps which resolves all the issues. Step 1: use the version in dependencies

testImplementation"org.robolectric:robolectric:4.0" in app.gradle    

step 2: In app.gradle inside android use below

testOptions {    
    unitTests {    
        includeAndroidResources = true      
    }    
}    

step 3:in gradle.properties paste the below

enableUnitTestBinaryResources=true

It works !!!

Zain
  • 37,492
  • 7
  • 60
  • 84
prasanna
  • 159
  • 1
  • 3
  • 14
1

You Can refer the same issue here:https://github.com/robolectric/robolectric/issues/1648

You should mention this in your test code:

@Config(manifest = "src/main/AndroidManifest.xml", emulateSdk = 18)

This tells the robolectric where your manifest file is. This should work unless you change your directory values.

also try to use the template available on the internet.Everything is setup in there.You don't need to change your directory values. You can get it from here:https://github.com/robolectric/deckard-gradle

Edit: The way i see is the exception occurs at "build/intermediates/bundles/debug/AndroidManifest.xml " but you are saying the file is at "app/build/intermediates/manifests/full/debug/AndroidManifest.xml",I guess these two are different locations. Please make sure you have the xml file at build/intermediates/bundles/debug/AndroidManifest.xml "

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
0

That's correct @Christine. I am also facing the exact problem. And the thing is build process is not creating bundles folder at all under intermediates. And custom robolectric runner also didn't work. Here is the custom runner I used. I haven't still figured out the solution but it seems that the problem may be related to build tool version that needs to used.

[Update] Changing working directory in my unit test run configuration to point to the module root directory which is being tested worked for me.[Update]

kaps
  • 190
  • 3
  • 9
0

I had this error after I updated AndroidStudio to 2.3.3 and my gradle plugin was updated too. It was solved by setting working directory to $MODULE_DIR$ and also updating robolectric dependency from testCompile 'org.robolectric:robolectric:3.0' to testCompile 'org.robolectric:robolectric:3.4.2'.

Note that setting working directory while robolectric version is old won't resolve it.

Akram
  • 2,158
  • 1
  • 17
  • 24