android – FragmentActivity Junit测试

前端之家收集整理的这篇文章主要介绍了android – FragmentActivity Junit测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用了 android-support-v4.jar的 Android兼容包Fragment.但我不能对此进行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扩展了Activity)
我在我的测试和主项目中使用了相同的android-support-v4.jar

解决方法

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

然后,您将能够编写如下测试:

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

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

猜你在找的Android相关文章