1
我正在使用ActivityInstrumentationTestCase2编写活动的测试用例。 在此活动中,我使用GoogleApiClient获取用户的位置。我想断言GoogleApiClient已连接。在Android Instrumentation TestCase中测试GoogleApiClient
这是我写
@RunWith(AndroidJUnit4.class)
public class SplashActivityTest
extends ActivityInstrumentationTestCase2<SplashScreenActivity>{
private SplashScreenActivity splashScreenActivity;
private TextView messageText;
private ProgressBar progressBar;
private boolean isLocationCallbackCalled;
private long LOCATION_TIMEOUT = 10000;
private GoogleApiClient mGoogleApiClient;
public SplashActivityTest() {
super(SplashScreenActivity.class);
}
@Before public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
splashScreenActivity = getActivity();
messageText = (TextView) splashScreenActivity.findViewById(R.id.textView2);
progressBar = (ProgressBar) splashScreenActivity.findViewById(R.id.progressBar);
mGoogleApiClient =
new GoogleApiClient.Builder(getInstrumentation().getContext())
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Test public void testGoogleApiClientConnected() {
assertEquals("Google api client not connected", mGoogleApiClient.isConnected(), true);
}
}
测试用例,但在运行的TestCase
java.lang.IllegalArgumentException: isGooglePlayServicesAvailable should only be called with Context from your application's package. A previous call used package 'com.example.myapp' and this call used package 'com.example.myapp.test'.
at com.google.android.gms.common.zze.zzan(Unknown Source)
at com.google.android.gms.common.zze.isGooglePlayServicesAvailable(Unknown Source)
at com.google.android.gms.common.zzc.isGooglePlayServicesAvailable(Unknown Source)
at com.google.android.gms.common.api.internal.zzh$zzb.zzpt(Unknown Source)
at com.google.android.gms.common.api.internal.zzh$zzf.run(Unknown Source)