Roboletric 4.0.1 不断在 buildActivity 上抛出 Resources$NotFoundException [英] Roboletric 4.0.1 keeps throwing Resources$NotFoundException on buildActivity

查看:31
本文介绍了Roboletric 4.0.1 不断在 buildActivity 上抛出 Resources$NotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 roboletric 构建非常简单的单元测试,但我无法创建活动.

I am trying to build very simple unittest with roboletric but I can't create the activity.

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

I tried what was suggested on the thread but no success.

资源$NotFoundException 调用 Robolectric.buildActivity() 时

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

Yet, I am using the 4.0.1 version. I wonder if there some extra setup that I need to do.

这是我的毕业证书:

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 更新为:

I did click on that link and update the gradle.properties to:

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

但没有成功.我仍然无法创建活动并且旧模式消息不断显示.

But no success. I still can't create the activity and the legacy mode message keeps showing up.

任何线索我做错了什么?

any clues what am I doing wrong?

谢谢.

推荐答案

Robolectric 无法找到您应用的资源.

Robolectric can't find your app's resources.

将此添加到您的build.gradle:

android {
    testOptions {
        unitTests.includeAndroidResources = true
    }
}

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

Add this to your gradle.properties (not necessary with Android Studio 3.3+):

android.enableUnitTestBinaryResources=true

这篇关于Roboletric 4.0.1 不断在 buildActivity 上抛出 Resources$NotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆