gpt4 book ai didi

android - Robolectric 3.0 测试 - Android

转载 作者:行者123 更新时间:2023-11-29 20:22:04 25 4
gpt4 key购买 nike

我对支持 Lollipop 的 Robolectric 3.0(最新)进行了以下测试:

package com.example.calculator.ui.fragments;


import android.os.Build;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.util.FragmentTestUtil;

import com.example.calculator.BuildConfig;

import static junit.framework.Assert.assertNotNull;

@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP, packageName = "com.example.calculator")
@RunWith(RobolectricGradleTestRunner.class)
public class CalcCalculationButtonFragTest {

@Test
public void validateButton() {

CalcCalculationButtonFrag fragment = new CalcCalculationButtonFrag();
FragmentTestUtil.startVisibleFragment(fragment);
assertNotNull(fragment);
}
}

此测试失败,因为在 onAttach() 中,我强制父 Activity 实现一个回调接口(interface),该接口(interface)将在 fragment 中触发操作时使用。我收到错误:

java.lang.ClassCastException: org.robolectric.util.FragmentTestUtil$FragmentUtilActivity@58059bf5 must implement OnCalculateSelectedListener
at com.example.calculator.ui.fragments.CalcCalculationButtonFrag.onAttach(CalcCalculationButtonFrag.java:47)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:853)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:833)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:130)
at org.robolectric.shadows.ShadowMessageQueue.access$100(ShadowMessageQueue.java:30)
at org.robolectric.shadows.ShadowMessageQueue$1.run(ShadowMessageQueue.java:95)
at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:230)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:85)
at org.robolectric.shadows.ShadowMessageQueue.enqueueMessage(ShadowMessageQueue.java:116)
at android.os.MessageQueue.enqueueMessage(MessageQueue.java)
at android.os.Handler.enqueueMessage(Handler.java:631)
at android.os.Handler.sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageDelayed(Handler.java:570)
at android.os.Handler.post(Handler.java:326)
at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1358)
at android.app.BackStackRecord.commitInternal(BackStackRecord.java:728)
at android.app.BackStackRecord.commit(BackStackRecord.java:704)
at org.robolectric.util.FragmentTestUtil.startVisibleFragment(FragmentTestUtil.java:25)
at com.example.calculator.ui.fragments.CalcCalculationButtonFragTest.validateButton(CalcCalculationButtonFragTest.java:24)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)


Process finished with exit code -1

其中“OnCalculateSelectedListener”是父 Activity 中所需的回调方法实现。

我应该如何创建或设置 fragment 以便测试通过?提前感谢您提供任何详细的反馈。

最佳答案

查看Robolectric源代码:https://github.com/robolectric/robolectric/blob/master/robolectric/src/main/java/org/robolectric/util/FragmentTestUtil.java

您编写的 CalcCalculationButtonFrag 强制 FragmentUtilActivity 必须实现 OnCalculateSelectedListener。为什么不提供您自己的 Activity?

尝试这样的事情:

public static extends FragmentActivity implements OnCalculateSelectedListener { ... }

然后在你的测试中:

@Test
public void validateButton() {

CalcCalculationButtonFrag fragment = new CalcCalculationButtonFrag();
FragmentTestUtil.startVisibleFragment(fragment, TestActivity.class);
assertNotNull(fragment);
}

关于android - Robolectric 3.0 测试 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33047008/

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