1

google-play-services_lib我有一个使用 Google Maps Android API v2 ( )的外部库项目的 Android 项目。

我也有测试项目,它首先测试我的项目形式。

package third.task.android.test;

import third.task.android.CompassActivity;
import third.task.android.R;
import android.app.Activity;
import android.content.Intent;
import android.test.ActivityUnitTestCase;

public class CompassActivityTest extends ActivityUnitTestCase<CompassActivity> {

    public CompassActivityTest() {
        super(CompassActivity.class);
    }

    public void testTruth() {
        assertTrue(true);
    }

    public void testPreConditions() {
        startActivity(new Intent(getInstrumentation().getTargetContext(), CompassActivity.class), null, null);
        Activity activity = getActivity();
        assertNull(activity.findViewById(R.id.compasRadar));
    }
}

这是我的失败跟踪,实际上显示了有关加载的错误class="com.google.android.gms.maps.MapFragment"

android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
at android.app.Activity.setContentView(Activity.java:1924)
at third.task.android.CompassActivity.onCreate(CompassActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
at third.task.android.test.CompassActivityTest.testPreConditions(CompassActivityTest.java:19)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:177)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1608)
Caused by: java.lang.IllegalStateException: Not on the main thread
at maps.am.r.b(Unknown Source)
at maps.ar.d.b(Unknown Source)
at maps.y.ae.setCompassEnabled(Unknown Source)
at maps.y.ae.a(Unknown Source)
at maps.y.ae.a(Unknown Source)
at maps.y.bu.a(Unknown Source)
at maps.y.p.onCreateView(Unknown Source)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$Stub.onTransact(IMapFragmentDelegate.java:107)
at android.os.Binder.transact(Binder.java:326)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
at com.google.android.gms.maps.MapFragment$b.onCreateView(Unknown Source)
at com.google.android.gms.internal.d$4.a(Unknown Source)
at com.google.android.gms.internal.d.a(Unknown Source)
at com.google.android.gms.internal.d.onCreateView(Unknown Source)
at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:807)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1112)
at android.app.Activity.onCreateView(Activity.java:4857)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
... 24 more

当我删除负责在布局中显示地图的代码以及与地图相关的代码片段时,一切正常,但我必须使用包括地图在内的全部功能测试我的应用程序。有没有办法做到这一点?

4

1 回答 1

3

怎么样ActivityInstrumentationTestCase2?无需启动activity,即可简单使用getActivity()。如果您需要使用自定义启动意图,您可以使用setActivityIntent(i)in指定它setUp()。我已经测试了它的工作:

package com.grt_team.airplanemode.test;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;

import com.grt_team.airplanemode.MainActivity;
import com.grt_team.airplanemode.R;

public class Test extends ActivityInstrumentationTestCase2<MainActivity> {

    public Test() {
        super(MainActivity.class);
    }

    public void testTruth() {
        assertTrue(true);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        //setActivityIntent(i);
    }

    public void testPreConditions() {
        Activity activity = getActivity();

        assertNotNull(activity);
        assertNull(activity.findViewById(0));
        assertNotNull(activity.findViewById(R.id.map_fragment));
    }

}
于 2013-03-15T17:02:19.750 回答