【问题标题】:Manifest and setup issues getting Robolectric working with Android Studio 1.1.0让 Robolectric 与 Android Studio 1.1.0 一起工作的清单和设置问题
【发布时间】:2015-02-24 15:55:56
【问题描述】:

我正在尝试进行 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 插件(unit 测试工作正常),但我不确定我缺少什么来启动和运行仪器测试.

应用级构建文件:

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

【问题讨论】:

  • 不完全确定我们是否有同样的问题。我想使用 Robolectric 2.4,但它不适用于 Android Studio 1.0.something。最后,我还在使用 robolectric 2.3。

标签: android testing robolectric robolectric-gradle-plugin


【解决方案1】:

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

【讨论】:

  • 感谢您的回复和文章。它确实帮助了我,同时“教我如何钓鱼”,让我更好地了解幕后的工作原理。
【解决方案2】:

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

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

但也请阅读@nenick 指南

【讨论】:

  • 这肯定让我更接近 - 路径加上注释上的其他属性肯定有帮助(现在我在资源中的某些东西上得到了 NPE)。我很快就会浏览指南。
  • 我假设您使用的是 App compat。检查阴影菜单项的点
  • 我们目前没有使用任何 AppCompat 库,但我遇到的问题之一是清单中 Google Play 服务版本的定义。 Google 的文档中有示例,其中该值应为整数,但这会使 RE 中的 AndroidManifest 类抛出 NPE。如果我将版本的定义更改为字符串,它似乎就是这样。奇怪。
  • 这在从 Android Studio 中运行测试时有效,但在从命令行运行测试时无效。关于如何调整路径的任何想法?我尝试将 ${project.rootDir} 和 ${project.projectDir} 作为 src/... 的前缀
  • @BillMote,它是旧 Robolectric 版本的答案,因为现在你不需要玩 AndroidManifest 的路径
【解决方案3】:

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

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")

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

【讨论】:

  • 终于解决了我的问题。 “它确实适用于我的一个项目,但是当我打开一个新项目并设置测试环境时,弹出相同的错误消息” => 我也遇到了同样的情况
  • 我之前没有找到你的答案(可能是因为你的答案在底部)。它确实适用于我的一个项目,我在这里回答:stackoverflow.com/a/45571884/4802664 然后创建了一个新项目并得到了同样的错误(我花了 3 天时间修复,第 3 天得到了你的答案)。现在我将在我的答案中链接您的答案。
【解决方案4】:

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

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 {

这里我们有指向清单文件和包名的简单链接

【讨论】:

    【解决方案5】:

    我刚改成:

    @RunWith(RobolectricTestRunner.class)
    

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

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-07
        相关资源
        最近更新 更多