簡體   English   中英

Robolectric 3.0測試-Android

[英]Robolectric 3.0 Test - Android

我使用支持棒棒糖的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實施一個回調接口,該接口將在片段中觸發動作時使用。 我收到錯誤消息:

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中必需的回調方法實現。

我應該如何創建或設置片段以使測試通過? 在此先感謝您提供任何詳細的反饋。

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

您編寫了CalcCalculationButtonFrag ,它迫使FragmentUtilActivity必須實現OnCalculateSelectedListener 為什么不提供自己的活動?

嘗試這樣的事情:

public static extends FragmentActivity implements OnCalculateSelectedListener { ... }

然后在您的測試中:

@Test
public void validateButton() {

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM