FragmentActivity 无法通过 ActivityInstrumentationTestCase2 进行测试 [英] FragmentActivity can not be tested via ActivityInstrumentationTestCase2

查看:23
本文介绍了FragmentActivity 无法通过 ActivityInstrumentationTestCase2 进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在针对使用最近发布的 Fragment 支持 API 的 android 应用程序执行 android 单元测试时遇到问题.当针对 FragmentActivity 运行测试时,日志中会显示以下错误,并且该类无法加载.当针对相同的类运行时,但从 Activity 派生的一个类运行良好.这两个类都可以作为应用程序正常工作!这意味着当简单地调用它们时,它们都会正确显示其布局和功能.支持 jar 是构建路径的一部分并包含在项目中.

I have a problem executing android unit tests against android applications that utilize the recently released Fragment support API. When the test is run against a FragmentActivity the following error shows up in the log and the class fails to load. When run against an identical class, but one derived from Activity the test works fine. Both classes work correctly as apps! Meaning that when simply invoked they both display their layout and function correctly. The support jar is part of the build path and included in the project.

我遇到的问题是,利用 Fragment(并支持 pre3.0 android)的唯一方法是利用 FragmentActivity,但如果不包括自动化测试,那么这个库有什么用.

The problem I have is that the only way to utilize fragments (and support pre3.0 android) is to utilize FragmentActivity, but if that excludes automated testing then what good is this library.

4-05 18:00:11.276: WARN/dalvikvm(1095): Class resolved by unexpected DEX: Lcom/example/android/app/FragmentLayoutSupport;(0x406351a0):0x12e5c8 ref [Landroid/support/v4/app/FragmentActivity;] Landroid/support/v4/app/FragmentActivity;(0x406351a0):0x12e440
04-05 18:00:11.276: WARN/dalvikvm(1095): (Lcom/example/android/app/FragmentLayoutSupport; had used a different Landroid/support/v4/app/FragmentActivity; during pre-verification)
04-05 18:00:11.286: WARN/dalvikvm(1095): Unable to resolve superclass of Lcom/example/android/app/FragmentLayoutSupport; (49)
04-05 18:00:11.286: WARN/dalvikvm(1095): Link of class 'Lcom/example/android/app/FragmentLayoutSupport;' failed
04-05 18:00:11.286: ERROR/dalvikvm(1095): Could not find class 'com.example.android.app.FragmentLayoutSupport', referenced from method com.example.android.app.test.FrameLayoutTest.<init>
04-05 18:00:11.286: WARN/dalvikvm(1095): VFY: unable to resolve const-class 131 (Lcom/example/android/app/FragmentLayoutSupport;) in Lcom/example/android/app/test/FrameLayoutTest;

这是我为演示该问题而构建的代码.测试用例只是尝试实例化被测类:

Here is the code I constructed to demonstrate the issue. The test case simply tries to instantiate the class under test:

FrameLayoutTest.java    
public class FrameLayoutTest extends
            ActivityInstrumentationTestCase2<FragmentLayoutSupport> {
        public FrameLayoutTest() {
            super(FragmentLayoutSupport.class);
        }

    public void testActivityTestCaseSetUpProperly() {
        assertNotNull("activity should be launched successfully", getActivity());
    }
}

我创建的两个类如下,fragment_layout是一个空的LinearLayout:

The two classes I have created are as follows and fragment_layout is an empty LinearLayout:

FrameLayout.java
public class FragmentLayout extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_layout);
    }
}

FragmentLayoutSupport.java
public class FragmentLayoutSupport extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_layout);
    }
}

推荐答案

为此花了半夜时间,终于找到了解决方案.关键是:

I spent half the night on this, and finally found a solution. The key line is:

04-05 18:00:11.276, (Lcom/example/android/app/FragmentLayoutSupport; had used a different Landroid/support/v4/app/FragmentActivity; during pre-verification). 

问题是您在测试项目中使用的 android-support-v4.jar 与应用程序项目中的不同.从您的测试项目中删除对 android-support-v4.jar 的所有引用.然后转到您的应用程序项目 Properties->Java Build Path->Order and Export 并检查 android-support-v4.jar 以将其导出.现在两个项目都将使用同一个库,dalvik 不会抱怨.

The problem is that the android-support-v4.jar which you are using in your test project is different from that one in your application project. Remove all of the references to android-support-v4.jar from your test project. Then go to your application project Properties->Java Build Path->Order and Export and check android-support-v4.jar to export it. Now both projects will be using the same library, and dalvik won't complain.

这篇关于FragmentActivity 无法通过 ActivityInstrumentationTestCase2 进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆