6

我正在尝试进行 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!消息。最后,我认为这是测试运行时出现以下错误的原因:

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

我确实打开了实验选项,配置了正确的 Gradle 插件(单元测​​试工作正常),但我不确定我缺少什么来启动和运行仪器测试。

应用级构建文件:

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'
}
4

6 回答 6

10

我写了一个分步指南如何使用android studio 1.1.0启用robolectric http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html我猜你会的为您的问题找到答案。还有一个例子https://github.com/nenick/AndroidStudioAndRobolectric

于 2015-02-24T17:45:15.233 回答
2

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

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

但也请阅读@nenick 指南

于 2015-02-24T19:39:22.467 回答
2

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

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!

我在 stackOverFlow 上找到的许多答案告诉我将 Manifest 文件的路径添加到@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")

干得好!希望这个答案可以让你的生活更轻松。

于 2015-07-17T06:23:36.193 回答
2

我遇到了同样的错误。我们使用多种风格和构建类型因此,有一些步骤可以使其工作:

1)Android studio测试运行配置

构建变体菜单

您也必须在 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 {

在这里,我们有指向清单文件和 packageName 的简单链接

于 2015-11-25T09:56:28.433 回答
0

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

于 2015-07-17T14:37:22.090 回答
0

我刚改成:

@RunWith(RobolectricTestRunner.class)

它有效。所有其他配置的东西似乎都是不必要的。

于 2015-07-09T15:10:45.777 回答