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:
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'
}
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
In your case next change might help:
@Config(manifest = "src/main/AndroidManifest.xml", emulateSdk = 18, reportSdk = 18)
But read also @nenick guide
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.
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
You have to set working directory to $MODULE_DIR$ in Windows too. 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
I just changed to:
@RunWith(RobolectricTestRunner.class)
and it works. All the other config stuff seems to be unnecessary.
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
来源:https://stackoverflow.com/questions/28700379/manifest-and-setup-issues-getting-robolectric-working-with-android-studio-1-1-0