Robolectric 使用 Android Studio 1.1.0 的清单和设置问题

Manifest and setup issues getting Robolectric working with Android Studio 1.1.0

我正在尝试进行 Robolectric 测试并在我们当前的项目中工作,但运气不佳。我的偏好是在 Android Studio 1.1.0+ 中将它们添加到 运行。这是我的项目结构:

这是我的测试:

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);
    }

}

无论我做什么来尝试通过更改清单的路径让框架找到测试,它都找不到它 - 我收到 WARNING: No manifest file found at ./AndroidManifest.xml.Falling back to the Android OS resources only. 消息或 API Level XX is not supported - Sorry! 消息.最后,我认为这就是我在测试 运行s:

时出现以下错误的原因
android.content.res.Resources$NotFoundException: unknown resource 2130903074

我确实打开了实验选项,配置了正确的 Gradle 插件(unit 测试工作正常),但我不确定我'我缺少进行仪器测试和 运行ning。

应用级构建文件:

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'

顶级构建文件:

dependencies {
    classpath 'com.android.tools.build:gradle:1.1.0'
    classpath 'org.robolectric:robolectric-gradle-plugin:1.0.0'
}

我写了一个如何使用 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

启用 robolectric 的分步指南

对于您的情况,下一个更改可能会有所帮助:

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

但也请阅读@nenick 指南

我刚改成:

@RunWith(RobolectricTestRunner.class)

并且有效。所有其他配置似乎都是不必要的。

经过几天的挣扎,我终于找到了你问题的根本原因:

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!

我在 Whosebug 上找到的许多答案告诉我将清单文件的路径添加到 @Config,如下所示:

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

它确实适用于我的一个项目,但是当我打开一个新项目并设置测试环境时,弹出相同的错误消息。

这背后的原因可能是您的项目的工作目录不同。您应该首先找到您的工作目录,然后在您的@Config(maniest = "...").

中指定它的相对路径

我在哪里可以找到 Android Studio 中的工作目录?

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

以我的工作目录为例,我的工作目录是

"/Users/Bible/AndroidstudioProjects/MVP"

所以我的 AndroidManifest.xml 的路径应该是

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

给你!希望这个回答可以让您的生活更轻松。

如果您使用的是最新的 Android Studio(1.2.x),那么最好也使用 RobolectricGradleTestRunner.class 它专为不同的构建风格和测试风格而设计。您也不需要指定源清单。它将根据您 运行ning 的构建风格获取清单。当您有不同的构建环境(暂存、生产)并且您想要 运行 特定环境中的测试用例时,这特别有用

我遇到过同样的错误。我们使用多种口味和构建类型 因此,有一些步骤可以让它发挥作用:

1) Android 工作室测试 运行 配置

您还必须在 Windows 中将工作目录设置为 $MODULE_DIR$。 robolectric.org/getting-started/ 应该这么说.

2)单元测试应该这样注解:

@RunWith(RobolectricTestRunner.class) 
@Config(constants = BuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml", packageName = "com.example.yourproject") 
public class SomeFragmentTest {

这里我们有简单的 link 来显示文件和 packageName