android - Fragment注入(inject)时查看ClassCastException

标签 android robolectric

鉴于我正在使用这样的 AndroidAnnotations:

@EFragment(R.layout.fragment_music_home2)
public class MusicHomeFragment extends AppsBasedHomeFragment {
    @ViewById
    protected ExtRecyclerView pager;
}

MusicHomeFragment_ 的生成代码在某一时刻看起来像这样:

    pager = ((ExtRecyclerView) hasViews.findViewById(com.company.R.id.pager));

这导致堆栈跟踪如下:

java.lang.ClassCastException: com.company.view.ExtRecyclerView_ cannot be cast to com.company.view.ExtRecyclerView
    at com.company.music.MusicHomeFragment_.onViewChanged(MusicHomeFragment_.java:97)
    at org.androidannotations.api.view.OnViewChangedNotifier.notifyViewChanged(OnViewChangedNotifier.java:41)
    at com.company.music.MusicHomeFragment_.onViewCreated(MusicHomeFragment_.java:85)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1120)
    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1929)
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
    at com.company.utils.BaseActivity.onStart(BaseActivity.java:104)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
    at android.app.Activity.performStart(Activity.java:5253)
    at org.robolectric.util.ReflectionHelpers$3.run(ReflectionHelpers.java:162)
    at org.robolectric.util.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:305)
    at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:156)
    at org.robolectric.util.ComponentController$1.run(ComponentController.java:72)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:304)
    at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45)
    at org.robolectric.util.ComponentController.invokeWhilePaused(ComponentController.java:69)
    at org.robolectric.util.ActivityController.start(ActivityController.java:143)
    at org.robolectric.util.ActivityController.setup(ActivityController.java:210)
    at org.robolectric.Robolectric.setupActivity(Robolectric.java:46)
    at com.company.music.MusicHomeFragmentTest.showEmptyPageWhenNoContentsAreAvailable(MusicHomeFragmentTest.java:65)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:52)
    at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:638)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:98)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
    at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
    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:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

这是测试的样子:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 19)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@PrepareForTest(MyThemeManager_.class)
public class MusicHomeFragmentTest {

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Test
    public void showEmptyPageWhenNoContentsAreAvailable() {

        mockStatic(MyThemeManager_.class);

        final MyThemeManager_ themeManager = mock(MyThemeManager_.class);

        when(MyThemeManager_.getInstance_(Mockito.<Context>anyObject())).thenReturn(themeManager);

        MusicHomeActivity_ controller = Robolectric.setupActivity(MusicHomeActivity_.class);

        // TODO: Start the test.

        PowerMockito.verifyStatic();
    }
}

build.gradle:

testCompile "junit:junit:${JUNIT_VERSION}"
testCompile "org.mockito:mockito-core:${MOCKITO_VERSION}"
testCompile "org.powermock:powermock-module-junit4:${POWERMOCK_VERSION}"
testCompile "org.powermock:powermock-module-junit4-rule:${POWERMOCK_VERSION}"
testCompile "org.powermock:powermock-api-mockito:${POWERMOCK_VERSION}"
testCompile "org.powermock:powermock-classloading-xstream:${POWERMOCK_VERSION}"

testCompile "com.jakewharton:disklrucache:${DISKLRUCACHE_VERSION}"
testCompile "org.robolectric:robolectric:${ROBOLECTRIC_VERSION}"
testCompile "org.robolectric:shadows-core:${ROBOLECTRIC_VERSION}"
testCompile "org.robolectric:shadows-support-v4:${ROBOLECTRIC_VERSION}"

ANDROID_TEST_VERSION = 0.3
DEXMAKER_VERSION = 1.4-SNAPSHOT
ESPRESSO_VERSION = 2.2
JUNIT_VERSION = 4.12
MOCKITO_VERSION = 1.10.19
POWERMOCK_VERSION = 1.6.2
ROBOLECTRIC_VERSION = 3.0
UIAUTOMATOR_VERSION = 2.1.1

如何解决这个问题,以便测试可以完美执行?

最佳答案

测试需要使用 @PowerMockIgnore 规则来增强,以防止错误转换的类。它似乎已被模拟框架的组合覆盖。

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 19)
// ** CHANGE BELOW **
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*", "com.company.view.ExtRecyclerView_"})
@PrepareForTest(MyThemeManager_.class)
public class MusicHomeFragmentTest {

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Test
    public void showEmptyPageWhenNoContentsAreAvailable() {

        mockStatic(MyThemeManager_.class);

        final MyThemeManager_ themeManager = mock(MyThemeManager_.class);

        when(MyThemeManager_.getInstance_(Mockito.<Context>anyObject())).thenReturn(themeManager);

        MusicHomeActivity_ controller = Robolectric.setupActivity(MusicHomeActivity_.class);

        // TODO: Start the test.

        PowerMockito.verifyStatic();
    }
}

关于android - Fragment注入(inject)时查看ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31453311/

相关文章:

android - 动画遛狗

android - Robolectric 是单元测试还是集成测试?

android - 由于 numeorus ReceiverDispatcher 和 Activity 实例导致的 robolectric 内存泄漏

java - 登录网站而不使用网站首页?

java - 我应该使用哪种方法(最快)来选择元素?

android - 我可以假设大多数安卓设备都有 wifi direct 吗?

android - 在运行时创建的 Robolectric 测试中加载文件/ Assets

android - 设置自定义对话框的宽度以包装内容android

android - 是否可以使用 Robolectric 进行集成测试?

android - Robolectric 和 Android SDK 29