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) 

回答

0

我已經解決了它,我正在此錯誤。其實我必須通過應用程序上下文而不是測試應用程序上下文。

我們可以通過正在被像測試的應用程序的上下文中

public class SplashActivityTest 
    extends ActivityInstrumentationTestCase2<SplashScreenActivity> { 

private SplashScreenActivity splashScreenActivity; 
private TextView messageText; 
private ProgressBar progressBar; 
private boolean isLocationCallbackCalled; 
private long TIMEOUT_IN_MS = 10000; 
private GoogleApiClient mGoogleApiClient; 
// register next activity that need to be monitored. 
Instrumentation.ActivityMonitor homeActivityMonitor; 

public SplashActivityTest() { 
    super(SplashScreenActivity.class); 
} 

@Before public void setUp() throws Exception { 
    super.setUp(); 
    injectInstrumentation(InstrumentationRegistry.getInstrumentation()); 

    homeActivityMonitor = getInstrumentation().addMonitor(HomeScreenActivity.class.getName(), null, false); 

    splashScreenActivity = getActivity(); 
    messageText = (TextView) splashScreenActivity.findViewById(R.id.textView2); 
    progressBar = (ProgressBar) splashScreenActivity.findViewById(R.id.progressBar); 

    final CountDownLatch latch = new CountDownLatch(1); 
    mGoogleApiClient = 
     new GoogleApiClient.Builder(splashScreenActivity).addApi(LocationServices.API) 
      .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { 
      @Override public void onConnected(Bundle bundle) { 
       latch.countDown(); 
      } 

      @Override public void onConnectionSuspended(int i) { 
       latch.countDown(); 
      } 
      }) 
      .build(); 
    mGoogleApiClient.connect(); 
    latch.await(); 
} 

@Test public void testPreconditions() { 
    //Try to add a message to add context to your assertions. These messages will be shown if 
    //a tests fails and make it easy to understand why a test failed 
    assertNotNull("splashScreenActivity is null", splashScreenActivity); 
    assertNotNull("messageText is null", messageText); 
    assertNotNull("progressBar is null", progressBar); 
} 

@Test public void testPlayServiceVersion() { 
    splashScreenActivity.runOnUiThread(new Runnable() { 
    @Override public void run() { 
     assertEquals("Wrong PlayService version", true, 
      AppUtils.checkPlayServices(splashScreenActivity)); 
    } 
    }); 
} 

@Test public void testLocationRuntimePermissionsGranted() { 
    getInstrumentation().runOnMainSync(new Runnable() { 
    @Override public void run() { 
     assertEquals("NO GPS Permission Granted", PackageManager.PERMISSION_GRANTED, 
      ContextCompat.checkSelfPermission(splashScreenActivity, 
       android.Manifest.permission.ACCESS_FINE_LOCATION)); 
    } 
    }); 
} 

@Test public void testGoogleApiClientConnected() { 
    assertEquals("Google api client not connected", true, mGoogleApiClient.isConnected()); 
} 

@After public void tearDown() { 
    getInstrumentation().removeMonitor(homeActivityMonitor); 
} 

} 
相關問題