Unit testing Roboletric 4.0.1一直在buildActivity上抛出资源$NotFoundException

Unit testing Roboletric 4.0.1一直在buildActivity上抛出资源$NotFoundException,unit-testing,robolectric,Unit Testing,Robolectric,我试图用roboletric构建非常简单的单元测试,但我无法创建活动 我尝试了线程上的建议,但没有成功 然而,我使用的是4.0.1版本。我想知道是否有一些额外的设置,我需要做 这是我的毕业证书: dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementat

我试图用roboletric构建非常简单的单元测试,但我无法创建活动

我尝试了线程上的建议,但没有成功

然而,我使用的是4.0.1版本。我想知道是否有一些额外的设置,我需要做

这是我的毕业证书:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'

    testImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    testImplementation 'androidx.test:core:1.0.0'
    testImplementation 'androidx.test.ext:junit:1.0.0'
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-core:2.23.0'
    testImplementation 'org.robolectric:robolectric:4.0.1'
    androidTestImplementation 'com.google.truth:truth:0.42'
    testImplementation 'com.google.truth:truth:0.42'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
我的测试程序是:

@Config(sdk = 28, manifest=Config.NONE)
@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {
    private Activity activity;
    @Before
    public void setup() {

        try {
            // I tried these 3 lines of code and they all throw the exception :(

            //activity = Robolectric.buildActivity(MainActivity.class).create().get();
            //activity = Robolectric.setupActivity(MainActivity.class);
            activity = Robolectric.buildActivity(MainActivity.class).create().start().resume().get();
        }
        catch (Exception e)
        {
            System.out.println("Keeps throwing exception!");
        }
    }
}
以下是输出:

[Robolectric] com.guitarv.robotest.MainActivityTest.validateTextViewContent: sdk=28; resources=legacy
[Robolectric] NOTICE: legacy resources mode is deprecated; see http://robolectric.org/migrating/#migrating-to-40
Keeps throwing exception!
Test:Sun Nov 11 06:21:59 PST 2018
我确实单击了该链接并将gradle.properties更新为:

org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.enableUnitTestBinaryResources=true
但是没有成功。我仍然无法创建活动,并且旧模式消息不断显示

有什么线索表明我做错了什么


谢谢。

Robolectric找不到您的应用程序资源

将此添加到build.gradle中:

将此添加到您的gradle.properties中,Android Studio 3.3+不需要:

android.enableUnitTestBinaryResources=true

Robolectric找不到你的应用程序的资源

将此添加到build.gradle中:

将此添加到您的gradle.properties中,Android Studio 3.3+不需要:

android.enableUnitTestBinaryResources=true

我在使用Robolectric 4.2时遇到了这个问题。升级到4.2.1解决了这个问题。

我在使用Robolectric 4.2时遇到了这个问题。升级到4.2.1修复了该问题。

无需使用@Configsdk=28作为robolectric选择SDK作为项目默认SDK,在您的情况下,我猜是28和。我遇到了一个类似的问题,通过将测试类SDK更改为27@Configsdk=27I解决了这个问题,但没有成功。让一个简单的测试程序运行起来是多么困难的事情:您是否确保包含文档中提到的android资源?testOptions{unitTests{includeAndroidResources=true}就是这样!谢谢!没有必要使用@Configsdk=28作为robolectric选择SDK作为项目默认SDK,在您的情况下,我猜是28和。我遇到了一个类似的问题,通过将测试类SDK更改为27@Configsdk=27I解决了这个问题,但没有成功。让一个简单的测试程序运行起来是多么困难的事情:您是否确保包含文档中提到的android资源?testOptions{unitTests{includeAndroidResources=true}就是这样!谢谢!仍然获取输出仍然获取输出这与添加android.enableUnitTestBinaryResources=true到gradle.properties结合使用这与添加android.enableUnitTestBinaryResources=true到gradle.properties结合使用