我正在使用 ActivityInstrumentationTestCase2 编写活动测试用例。在此活动中,我使用 GoogleApiClient 来获取用户的位置。我想要 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)