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)