android-Robolectric 3 GooglePlayServicesNotAvailableException
作者:互联网
我刚刚开始使用Robolectric,想知道如何解决Google Play服务.我正在使用Robolectric 3 RC2,而我的成绩如下:
build.bradle
compile 'com.squareup.okhttp:okhttp:2.3.0'
compile 'com.google.android.gms:play-services:7.0.0'
testCompile ("org.robolectric:robolectric:3.0-rc2"){
exclude module: 'commons-logging'
exclude module: 'httpclient'
}
testCompile ("org.robolectric:shadows-play-services:3.0-rc2"){
exclude module: 'commons-logging'
exclude module: 'httpclient'
}
testCompile 'com.squareup.okhttp:mockwebserver:1.2.1'
我正在从项目中运行一种方法,以从API获取json.调用时,我会传递广告ID:
AdvertisingIdClient.Info adInfo = null;
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
} catch (IOException |
GooglePlayServicesNotAvailableException |
GooglePlayServicesRepairableException e) {
// Unrecoverable error connecting to Google Play services (e.g.,
// the old version of the service doesn't support getting AdvertisingId).
e.printStackTrace();
}
我测试的主要部分是:
@Test(timeout = 7000)
public void jsonLoading() {
try {
client.loadData(this);
} catch (Exception e){
ex = e;
}
assertNull(ex);
//wait for task code
ShadowApplication.runBackgroundTasks();
每次运行测试时,我都会收到GooglePlayServicesNotAvailableException(来自e.printStackTrace();),并且无法解决它并断言它,因此测试将无法通过.
我还没有找到调试我的代码的任何线索.
解决方法:
前几天我碰到了这个.
采用:
testCompile 'org.robolectric:shadows-play-services:3.0'
testCompile 'org.robolectric:shadows-support-v4:3.0'
并查看以下示例:
public class TestBase {
@Before
public void setUp() throws Exception {
// Turn off Google Analytics - Does not need to work anymore
final ShadowApplication shadowApplication = Shadows.shadowOf(RuntimeEnvironment.application);
shadowApplication.declareActionUnbindable("com.google.android.gms.analytics.service.START");
// Force success
ShadowGooglePlayServicesUtil.setIsGooglePlayServicesAvailable(ConnectionResult.SUCCESS);
}
@After
public void tearDown() throws Exception {
}
}
public MyTestClass extends TestBase {
}
标签:google-play-services,junit,android-testing,robolectric,android 来源: https://codeday.me/bug/20191120/2043763.html