使用Android Studio 1.1.0时,Robolectric的清单文件和设置问题。

6
我正在尝试在我们当前的项目中使用Robolectric测试,并且并不顺利。我希望能够在Android Studio 1.1.0+内运行这些测试。 这是我的项目结构: enter image description here 这是我的测试代码:
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'
}

不确定我们是否有相同的问题。我想使用Robolectric 2.4,但它与Android Studio 1.0.x版本不兼容。最终,我仍在使用robolectric 2.3。 - Leon Joosse
6个回答

10

感谢您的回复和文章。它确实帮助了我,同时也“教会了我如何钓鱼”,让我更好地了解了幕后的工作原理。 - TheIcemanCometh

2

我曾经遇到过同样的错误。我们使用了多种flavors和buildtypes,因此需要以下步骤来使其正常运行:

1)Android Studio测试运行配置

Build Variants menu

你需要在Windows中将工作目录设置为$MODULE_DIR$。robolectric.org/getting-started/应该会有相关说明。

Run Configuration Menu

2)单元测试应该像这样进行注释:

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

这里有一个指向清单文件和包名的普通链接。

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

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


终于解决了我的问题。“它在我的一个项目中确实有效,但当我打开一个新项目并设置测试环境时,出现了同样的错误消息”,这对我也是一样的。 - MD TAREQ HASSAN
我之前没有找到你的答案(可能是因为你的答案在底部)。它对我的一个项目起作用了,我在这里回答了:https://dev59.com/hJ_ha4cB1Zd3GeqPvTgv#45571884 然后我创建了一个新项目,得到了相同的错误(花了我3天时间来修复,在第三天得到了你的答案)。现在我会在我的答案中链接你的答案。 - MD TAREQ HASSAN

2
在您的情况下,下一个更改可能会有所帮助:
@Config(manifest = "src/main/AndroidManifest.xml", emulateSdk = 18, reportSdk = 18)

但是也要阅读@nenick指南。

这确实让我更接近了目标——路径以及注释中的其他属性肯定有所帮助(现在我在资源中遇到了NPE)。我很快就会仔细阅读指南。 - TheIcemanCometh
我假设你正在使用App compat。检查阴影菜单项的位置。 - Eugen Martynov
我们目前没有使用任何AppCompat库,但我遇到的一个问题是在清单文件中定义Google Play服务版本。Google的文档中有例子,该值应为整数,但这会导致RE中的AndroidManifest类抛出NPE。如果我将版本的定义更改为字符串,则似乎可以解决该问题。奇怪。 - TheIcemanCometh
这在Android Studio中运行测试时有效,但在从命令行运行测试时无效。有没有关于如何调整路径的想法?我尝试使用${project.rootDir}和${project.projectDir}作为src/...的前缀。 - Bill Mote
@BillMote,这是针对旧的Robolectric版本的答案,现在你不需要玩弄AndroidManifest路径了。 - Eugen Martynov

0

我刚刚改成了:

@RunWith(RobolectricTestRunner.class)

它可以工作。所有其他配置似乎都是不必要的。


0
如果您正在使用最新的Android Studio(1.2.x),那么最好使用RobolectricGradleTestRunner.class。它专为不同的构建风格和测试风格设计。您也不需要指定源清单文件。它将根据您正在运行的构建风格来选择清单文件。当您拥有不同的构建环境(暂存,生产)并且想在特定环境中运行测试用例时,这是非常有用的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接