简体   繁体   English

使用Robolectric与Android Studio 1.1.0一起工作的清单和设置问题

[英]Manifest and setup issues getting Robolectric working with Android Studio 1.1.0

I am trying to get Robolectric tests up and working in our current project, and not having a lot of luck. 我正在努力让Robolectric测试并在我们当前的项目中工作,而且没有太多运气。 My preference would be to get these to run within Android Studio 1.1.0+. 我的偏好是让它们在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. 无论我做什么尝试通过更改清单的路径来获取框架来查找测试,它都找不到它 - 要么我得到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! 消息或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. 我确实打开了实验选项,配置了正确的Gradle插件( 单元测试工作正常),但我不确定我缺少什么来启动和运行测试测试。

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. 我写了一步一步指导如何使用android studio 1.1.0启用robolectric http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html我猜你会的找到你的问题的答案。 And an example https://github.com/nenick/AndroidStudioAndRobolectric 还有一个例子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 但也请阅读@nenick指南

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: 我在stackOverFlow上找到的许多答案告诉我将Manifest文件的路径添加到@Config,如下所示:

@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 = "..."). 您应首先找到您的工作目录,然后在@Config(maniest =“...”)中指定它的相对路径

Where can I find Working directory in Android Studio? 我在哪里可以找到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 所以我的AndroidManifest.xml的路径应该是

@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 1)Android studio测试运行配置

Build Variants菜单

You have to set working directory to $MODULE_DIR$ in Windows too. 您还必须在Windows中将工作目录设置为$ MODULE_DIR $。 robolectric.org/getting-started/ should say that. robolectric.org/getting-started/应该这么说。

运行配置菜单

2) Unit test should be annotated like this: 2)单元测试应该注释如下:

@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 这里我们有清单文件和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. 如果您使用的是最新的Android Studio(1.2.x),那么它也可以使用RobolectricGradleTestRunner.class。它专为不同的构建风格和测试风格而设计。 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 当您具有不同的构建环境(登台,生产)并且希望在特定环境中运行测试用例时,这尤其有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM