1
我有Robolectric 3.0(最新的),它支持棒棒堂以下測試: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()我逼父活動來實現回調接口測試失敗當片段中的動作被觸發時將使用它。我收到錯誤:
java.lang.ClassCastException: [email protected]bf5 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」是父活動中必需的回調方法實現。
我應該如何創建或設置片段以便測試通過?提前感謝任何詳細的反饋。
爲'TestFragment'顯示的代碼。你能告訴我們實際的錯誤嗎? –
@JaredBurrows上面顯示了完整的測試和錯誤報告。我很確定問題出在測試「設置」過程中,我應該包含父級活動,以便可以測試片段。片段強制父活動實現所需的回調接口。我從以下鏈接討論了我從Google工程師那裏找到的唯一參考資料,但未透露實施情況。鏈接是[這裏](https://groups.google.com/forum/#!topic/robolectric/g-hx0uRhEG4)。 – CBA110