FragmentActivity 无法通过 ActivityInstrumentationTestCase2 进行测试
我在针对利用最近发布的 Fragment 支持 API 的 Android 应用程序执行 Android 单元测试时遇到问题。当针对 FragmentActivity 运行测试时,日志中会显示以下错误,并且类无法加载。当针对相同的类(但派生自 Activity 的类)运行时,测试工作正常。这两个类都可以作为应用程序正常工作!这意味着,当简单调用时,它们都会正确显示其布局和功能。支持 jar 是构建路径的一部分并包含在项目中。
我遇到的问题是,利用片段(并支持 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);
}
}
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.
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());
}
}
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);
}
}
And
FragmentLayoutSupport.java
public class FragmentLayoutSupport extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此我折腾了半个晚上,终于找到了解决办法。关键是:
问题是您在测试项目中使用的 android-support-v4.jar 与您的应用程序项目中的不同。从测试项目中删除对 android-support-v4.jar 的所有引用。然后进入您的应用程序项目属性->Java构建路径->订单和导出并选中android-support-v4.jar将其导出。现在两个项目都将使用相同的库,dalvik 不会抱怨。
I spent half the night on this, and finally found a solution. The key line is:
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.
Rupert 的 IntelliJ 回答并没有让我完全明白。我通过像 Eclipse 答案建议的那样导出 jar 解决了这个问题。
文件>项目结构>模块> [选择您的主应用程序]>依赖项选项卡>单击支持 jar 旁边的导出复选框
The IntelliJ answer by Rupert didn't get me all the way there. I solved this by exporting the jar like the Eclipse answer suggested.
File > Project Structure > Modules > [select your main app] > dependencies tab > click export check box next to the support jar
对于遇到此问题的任何 IntelliJ 用户,等效的解决方法是将依赖项的范围设置为“已提供”,如下所示:
File > >项目结构>模块> [选择您的测试应用程序]>依赖项选项卡>在范围下拉列表中选择“已提供”。
For any IntelliJ users who run into this problem the equivalent fix is to set the scope of your dependency to 'Provided' as follows:
File > Project Structure > Modules > [select your test app] > dependencies tab > select 'Provided' in scope drop down.