Manifest and setup issues getting Robolectric working with Android Studio 1.1.0
Asked Answered
M

6

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'
}
Marchpane answered 24/2, 2015 at 15:55 Comment(1)
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.Equities
P
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

Pryor answered 24/2, 2015 at 17:45 Comment(1)
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.Marchpane
A
2

In your case next change might help:

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

But read also @nenick guide

Apophysis answered 24/2, 2015 at 19:39 Comment(5)
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.Marchpane
I assume you are using App compat. Check the point for the shadow menu itemApophysis
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.Marchpane
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/...Brigitte
@BillMote, it is answer for old Robolectric version, as for now you don't need to play with path to AndroidManifestApophysis
S
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.

Shofar answered 17/7, 2015 at 6:23 Comment(2)
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 meTolbooth
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://mcmap.net/q/1472522/-resources-notfoundexception-when-running-roboelectric-test 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.Tolbooth
F
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

Forefoot answered 25/11, 2015 at 9:56 Comment(0)
M
0

I just changed to:

@RunWith(RobolectricTestRunner.class)

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

Merchandise answered 9/7, 2015 at 15:10 Comment(0)
C
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

Catacomb answered 17/7, 2015 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.