gpt4 book ai didi

android - 运行 Robolectric 测试时的 Resources$NotFoundException

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:11 24 4
gpt4 key购买 nike

我刚刚使用新应用设置了 Roboelectric 3.2.2,并且编写了我的第一个简单测试:

@RunWith(RobolectricTestRunner.class)
@Config(manifest="src/main/AndroidManifest.xml",
packageName="my.pacakge.name.debug")
public class MainActivityTest {

@Test
public void clickButton() {
MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);
String text = ((TextView)mainActivity.findViewById(R.id.text_main)).getText().toString();
assertEquals("Should equal Hello World!", "Hello World!", text);
}
}

我已按照所有设置和说明进行操作 herehere但是每次尝试运行测试时我仍然会收到此错误:

android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0 in packages [android, my.package.name.debug]

此异常发生在我调用 setupActivity() 的测试的第一行。

最佳答案

它对我有用: 添加 @Config(manifest = "src/main/AndroidManifest.xml")

@Config(manifest = "src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {

@Test
public void testSomething() throws Exception {
assertTrue(Robolectric.setupActivity(MainActivityTest .class) != null);
}
}

注意 1:如果您仍然遇到同样的错误(在创建新项目时发生)

The Reason is related to different working directory & the answer is here: https://stackoverflow.com/a/31469526/4802664

注2:使用

Android Studio 3.0 (canary 9)
Gradle 4.1
Android Gradle plugin 3.0.0-alpha9
robolectric: 3.4.2

Gradle

buildscript {

repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha9'
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.0.2'


testImplementation 'junit:junit:4.12'

// Espresso
androidTestImplementation 'com.android.support.test:runner:1.0.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0'

// Robolectric
testImplementation 'org.hamcrest:hamcrest-library:1.3'
testImplementation 'org.robolectric:robolectric:3.4.2'
}

关于android - 运行 Robolectric 测试时的 Resources$NotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42282028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com