我在针对使用最近发布的 Fragment 支持 API 的 android 应用程序执行 android 单元测试时遇到问题。当针对 FragmentActivity 运行测试时,日志中会显示以下错误,并且类无法加载。当针对相同的类运行但从 Activity 派生的类时,测试工作正常。这两个类都可以作为应用程序正常工作!这意味着当简单地调用它们时,它们都会正确显示它们的布局和功能。支持 jar 是构建路径的一部分并包含在项目中。
我遇到的问题是,使用 Fragment(并支持 pre3.0 android)的唯一方法是使用 FragmentActivity,但如果不包括自动化测试,那么这个库有什么用处。
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;
这是我为演示该问题而构建的代码。测试用例只是尝试实例化被测类:
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:
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);
}
}