当前位置:  开发笔记 > Android > 正文

在Espresso测试中模拟Intent Extras

如何解决《在Espresso测试中模拟IntentExtras》经验,为你挑选了1个好方法。

我正试图Activity在浓缩咖啡中推出.问题是我想把嘲笑的额外内容放到Intent我用来启动它的地方Activity.这是一个例子.

@RunWith(AndroidJUnit4.class)
public final class NiceActivityTester
{
    @Rule
    public final ActivityTestRule activityRule = new ActivityTestRule<>(NiceActivity.class, true, false);

    @Test
    public void justStartPlease() {
        NiceThing niceThing = Mockito.mock(NiceThing.class);
        Mockito.when(niceThing.getName()).thenReturn("Nice!");

        Intent intent = new Intent(InstrumentationRegistry.getTargetContext(), NiceActivity.class);
        intent.putExtra("NICE_THING", niceThing);

        activityRule.launchActivity(intent);
    }
}

不幸的是,解组Parcelable失败了.

java.lang.RuntimeException: Unable to start activity ComponentInfo{app.application/app.application.activity.NiceActivity}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: NiceThing_Proxy
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: NiceThing_Proxy
    at android.os.Parcel.readParcelableCreator(Parcel.java:2295)
    at android.os.Parcel.readParcelable(Parcel.java:2245)
    at android.os.Parcel.readValue(Parcel.java:2152)
    at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
    at android.os.BaseBundle.unparcel(BaseBundle.java:221)
    at android.os.Bundle.getParcelable(Bundle.java:755)
    at android.content.Intent.getParcelableExtra(Intent.java:5088)
    at app.application.NiceActivity.getNiceThing(NiceActivity.java:40)
    at app.application.NiceActivity.setUpToolbar(NiceActivity.java:30)
    at app.application.NiceActivity.onCreate(NiceActivity.java:20)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)

有没有办法使用模拟的附加功能Intent?也许关于这个领域的一些最佳实践?

我真的不想为我正在使用的每个模型制作构造函数,其中一些类与十几个字段相当复杂.间谍额外或Intent使用Mockito没有帮助.



1> Max..:

使用ActivityTestRule和覆盖getActivityIntent

在此处找到的示例

public class MainActivityLaunchIntentTest {

    @Rule
    public ActivityTestRule mActivityRule =
            new ActivityTestRule(MainActivity.class) {
                @Override
                protected Intent getActivityIntent() {
                    Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
                    Intent result = new Intent(targetContext, MainActivity.class);
                    result.putExtra("Name", "Earth");
                    return result;
                }
            };

    @Test
    public void shouldShowHelloEarth() {
        onView(withId(R.id.main__tv_hello)).check(matches(withText("Hello Earth!")));
    }
}

推荐阅读
李桂平2402851397
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有