gpt4 book ai didi

android - FragmentActivity Junit 测试

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:24 26 4
gpt4 key购买 nike

我使用了 Android 兼容包的 Fragment,使用了 android-support-v4.jar。但我不能对此进行 JUnit 测试。

我的主要FragmentActivity类声明如下

public class MyActivityClass extends FragmentActivity{
...............
}

然后在我的测试项目中

public class MyActivityClassTest extends ActivityInstrumentationTestCase2<MyActivityClass> {
public MyActivityClassTest() {
super("com.android.myproject", MyActivityClass.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
...................
}

public void testPreconditions() {
.................
}
public void testNotNull(){
................
}
}

但是当我作为 Android JUnit Test 运行时产生 FailedToCreateTests[Runner:Junit3]
故障跟踪

java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:239)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Caused by: java.lang.reflect.InvocationTargetException
at com.android.myproject.test.MyActivityClassTest.<init>(MyActivityClassTest.java:28)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:263)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:185)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:336)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3982)
at android.app.ActivityThread.access$2900(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1901)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.android.myproject.MyActivityClass
... 19 more

当我将 MyActivityClass 更改为扩展 Activity 时,它工作正常 (MyActivityClass extends Activity)
我在测试和主项目中使用了相同的 android-support-v4.jar

最佳答案

ActivityInstrumentationTestCase2Fragments 兼容。您应该只按照 Android Testing: External libraries 中提到的步骤进行操作,已更新以涵盖 android-support-v4.jar 的情况。

然后,您将能够编写这样的测试:

public void testFragmentManager() {
FragmentActivity activity = getActivity();
assertNotNull(activity.getSupportFragmentManager());
}

或者任何你需要测试的东西。

关于android - FragmentActivity Junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6491270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com