运行测试时,方法调用会导致ClassCastException

问题描述 投票:0回答:1

我正在尝试在一个父类扩展的片段中运行一些测试......我的问题是我有一个方法正在使用一个Activity类进行演员。

FragmentToTest extends ParentNotification来自extendsParentFragment

这是我在ParentFragment中的方法:

public Location getCurrentLocation(){
        return ((ParentLocationActivity) getActivity()).getCurrentLocation();
    }

然后,当我运行我的测试时,我收到此错误:

java.lang.RuntimeException: java.lang.ClassCastException: android.support.v4.app.FragmentActivity cannot be cast to com.myproject.activity.ParentLocationActivity

如果我回来,例如,没有。我可以完美地运行测试。但我需要用这种方法测试y片段。

我的问题是;是某种方法来修复测试中的错误或是某种方式可以从我的测试类或类似的东西看不到这个方法跳过这个方法?

请注意,此方法(getCurrentLocation())在需要时在整个应用程序中正常工作。

提前致谢

这是我的测试类:

public class CreateOfferPresenterTest extends FragmentActivity {


    @Rule
    public FragmentTestRule<?, CreateOfferFragment> fragmentTestRule = FragmentTestRule.create(CreateOfferFragment.class);


    @Test
    public void setUp(){
    }

}

android unit-testing mockito powermock
1个回答
0
投票

的错误消息

java.lang.RuntimeException:java.lang.ClassCastException:android.support.v4.app.FragmentActivity无法强制转换为com.myproject.activity.ParentLocationActivity

告诉你,你有一个android.support.v4.app.FragmentActivity对象,无法将其投射到com.myproject.activity.ParentLoationActivity。如果你打算垂头丧气,你需要确保ParentLocationActivity直接或传递地从FragmentActivity扩展。否则,您需要实现一个方法,根据ParentLocationActivity将您的FragmentActivity用于需要它,并通过您编写的源代码自己进行投射。

© www.soinside.com 2019 - 2024. All rights reserved.