清单和设置问题让Robolectric使用Android Studio 1.1.0
我正在尝试进行机器人分子测试,并在我们当前的项目中工作,但运气不太好。我的首选是让它们在AndroidStudio 1.1.0+中运行。这是我的项目结构: 这是我的测试:清单和设置问题让Robolectric使用Android Studio 1.1.0,android,testing,robolectric,robolectric-gradle-plugin,Android,Testing,Robolectric,Robolectric Gradle Plugin,我正在尝试进行机器人分子测试,并在我们当前的项目中工作,但运气不太好。我的首选是让它们在AndroidStudio 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.rob
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);
}
}
无论我如何尝试通过更改清单的路径来让框架找到测试,它都找不到它-或者我在./AndroidManifest.xml.找到了警告:没有清单文件。仅返回到Android OS资源。
消息,或者API级别XX不受支持-抱歉代码>消息。最后,我认为这就是我在测试运行时出现以下错误的原因:
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'
}
顶级生成文件:
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中启用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!
我在stackOverFlow上找到的许多答案告诉我将清单文件的路径添加到@Config,如下所示:
@Config(manifest = "src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml", packageName = "com.example.yourproject")
public class SomeFragmentTest {
它确实适用于我的一个项目,但当我打开一个新项目并设置测试环境时,同样的错误消息弹出
这背后的原因可能是项目的不同工作目录。您应该首先找到工作目录,然后在@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
它是为不同的构建风格和测试风格而设计的。您也不需要指定源清单。它将根据您正在运行的构建风格拾取清单。当您有不同的构建环境(登台、生产)并且希望在特定环境中运行测试用例时,这一点特别有用。我们使用几种风格和构建类型
因此,有一些步骤可以让它工作:
1) Android studio测试运行配置
您还必须在Windows中将工作目录设置为$MODULE_DIR$。robolectric.org/getting-started/应该这么说
2) 单元测试的注释如下:
@Config(manifest = "src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml", packageName = "com.example.yourproject")
public class SomeFragmentTest {
这里我们有到manifest file和packageName的普通链接,不完全确定是否存在相同的问题。我想使用Robolectric2.4,但在AndroidStudio 1.0.0上效果不好。最后,我仍然在使用robolectric 2.3。这肯定让我更接近了——路径加上注释上的其他属性肯定有帮助(现在我在参考资料中得到了NPE)。我将很快浏览指南。我假设您正在使用App compat。检查影子菜单项我们目前没有使用任何AppCompat库,但我遇到的问题之一是清单中Google Play服务版本的定义。Google的文档中有一些示例,其中该值应该是整数,但这使得AndroidManifest类重新抛出NPE。如果我将版本的定义更改为字符串,它似乎是这样的。奇怪。这在Android Studio中运行测试时有效,但在命令行中运行测试时无效。有没有关于如何调整路径的想法?我尝试了${project.rootDir}和${project.projectDir}作为src/..@BillMote的前缀,这是旧的Robolectric版本的答案,因为现在您不需要使用AndroidManifest的路径。感谢您的回复和文章。它确实帮助了我,同时“教会了我如何钓鱼”,让我更好地了解幕后的运作。最终解决了我的问题。“它确实适用于我的一个项目,但当我打开一个新项目并设置测试环境时,出现了相同的错误消息”=>meI之前也没有找到您的答案(可能是因为您的答案在底部)。它确实对我的一个项目有效&我在这里回答:然后创建了一个新项目并得到了相同的错误(我花了3天时间修复,第3天得到了你的答案)。现在我将把你的答案链接到我的答案中。