6

I am trying to get Robolectric tests up and working in our current project, and not having a lot of luck. My preference would be to get these to run within Android Studio 1.1.0+. This is my project structure:

enter image description here

and here is my test:

import android.widget.Button;

import com.mycompany.android.app.R;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static org.junit.Assert.assertNotNull;

@Config(manifest = "AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class SplashActivityTest {

    private SplashActivity splashActivity;

    @Before
    public void setup() {
        splashActivity = Robolectric.buildActivity(SplashActivity.class).create().start().resume().get();
    }

    @Test
    public void shouldNotBeNull() {
        Button signUpButton = (Button) splashActivity.findViewById(R.id.sign_up_button);
        assertNotNull(signUpButton);

        Button loginButton = (Button) splashActivity.findViewById(R.id.login_button);
        assertNotNull(loginButton);
    }

}

No matter what I do to try and get the framework to find the test by changing the path to the manifest, it cannot find it - either I get WARNING: No manifest file found at ./AndroidManifest.xml.Falling back to the Android OS resources only. messages, or API Level XX is not supported - Sorry! messages. In the end, I think this is the reason I'm getting the following errors when the test runs:

android.content.res.Resources$NotFoundException: unknown resource 2130903074

I do have the experimental option turned on, have the right Gradle plugin configured (unit tests work fine), but I'm not sure what I'm missing to get instrumentation tests up and running.

App-level build file:

apply plugin: 'org.robolectric'

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'org.robolectric:robolectric:2.4'
testCompile 'org.hamcrest:hamcrest-core:1.1'
testCompile 'org.hamcrest:hamcrest-library:1.1'
testCompile 'org.hamcrest:hamcrest-integration:1.1'

Top-level build file:

dependencies {
    classpath 'com.android.tools.build:gradle:1.1.0'
    classpath 'org.robolectric:robolectric-gradle-plugin:1.0.0'
}
TheIcemanCometh
  • 1,055
  • 2
  • 17
  • 31
  • Not entirely sure if we have the same problem. I wanted to use Robolectric 2.4, but that didn't work well with Android Studio 1.0.something. In the end, I am still using robolectric 2.3. – Leon Joosse Feb 24 '15 at 16:04

6 Answers6

10

I wrote a step by step guide how to enable robolectric with android studio 1.1.0 http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html I guess you will find an answer for your issue. And an example https://github.com/nenick/AndroidStudioAndRobolectric

nenick
  • 7,340
  • 3
  • 31
  • 23
  • Thanks for the reply and the article. It definitely helped me out, while at the same time "taught me how to fish" in regards to giving me a better idea of how things work behind the scenes. – TheIcemanCometh Feb 25 '15 at 12:14
2

In your case next change might help:

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

But read also @nenick guide

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
  • This definitely got me closer - the path plus the other attributes on the annotation definitely helped (now I get NPEs on something in the resources). I'll be going through the guide shortly. – TheIcemanCometh Feb 24 '15 at 19:58
  • I assume you are using App compat. Check the point for the shadow menu item – Eugen Martynov Feb 24 '15 at 20:26
  • We're not currently using any of the AppCompat libraries, but one of the issues I was having was the definition of the Google Play Services version in the manifest. The documentation from Google has examples where that value should be an integer, but that makes the AndroidManifest class in RE throw the NPE. If I change the definition of the version to a String it seems to like that. Strange. – TheIcemanCometh Feb 25 '15 at 15:29
  • This works when running tests from within Android Studio, but not when tests are run from the command line. Any thoughts on how to adjust the path? I tried ${project.rootDir} and ${project.projectDir} as a prefix to src/... – Bill Mote Mar 28 '17 at 13:27
  • @BillMote, it is answer for old Robolectric version, as for now you don't need to play with path to AndroidManifest – Eugen Martynov Mar 28 '17 at 15:59
2

After few days struggling, I eventually figured out the root cause of your question:

WARNING: No manifest file found at ./AndroidManifest.xml.Falling back to the Android OS resources only. messages, or API Level XX is not supported - Sorry!

Many answers I found on stackOverFlow told me to add the path of Manifest file to the @Config like this:

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

It did work for one of my projects, but when I opened a new project and set up the testing environment, same error message popped up.

The reason behind this would be the different working directory of your project. You should locate your working directory first, and then specify the relative path to it in your @Config(maniest = "...").

Where can I find Working directory in Android Studio?

Run -> Edit Configurations -> Junit -> Your Test 
-> Configuration Tab -> **Working directory:**

Take my working directory as an example, my working directory is

"/Users/Bible/AndroidstudioProjects/MVP"

so the path to my AndroidManifest.xml should be

@Config(manifest = "app/src/main/AndroidManifest.xml")

Here you go! Hope this answer can make your life easier.

pptang
  • 1,931
  • 1
  • 15
  • 18
  • Finally solved my problem. "It did work for one of my projects, but when I opened a new project and set up the testing environment, the same error message popped up" => same happened to me – MD TAREQ HASSAN Aug 10 '17 at 12:40
  • I did not find your answer before (may be because of your answer at bottom). It did work for one of my projects & and I answered here: https://stackoverflow.com/a/45571884/4802664 Then created a new project & got the same error (took me 3 days to fix, on the 3rd day got your answer). Now I will link your answer in my answer. – MD TAREQ HASSAN Aug 10 '17 at 12:41
2

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

1) Android studio tests run configuration

Build Variants menu

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

Run Configuration Menu

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

user1438038
  • 5,821
  • 6
  • 60
  • 94
Kirill Vashilo
  • 1,559
  • 1
  • 18
  • 27
0

I just changed to:

@RunWith(RobolectricTestRunner.class)

and it works. All the other config stuff seems to be unnecessary.

Meanman
  • 1,474
  • 20
  • 17
0

If you are using latest Android Studio(1.2.x), then its better too use RobolectricGradleTestRunner.class It's designed for different build flavors and test flavors. You also need not specify your source manifest. It will pickup the manifest based on your build flavor you are running. This is specially useful when you have different build environments(staging, production) and you want to run your test cases in a specific environment

Kanishk
  • 506
  • 3
  • 14