Robolectric 3 GooglePlayServicesNotAvailableException [英] Robolectric 3 GooglePlayServicesNotAvailableException

查看:425
本文介绍了Robolectric 3 GooglePlayServicesNotAvailableException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Robolectric,并想知道如何解决 Google Play服务。我使用的是 Robolectric 3 RC2 ,我的gradle如下:
$ b build.bradle

  compile'c​​om.squareup.okhttp:okhttp:2.3.0'
compile'c​​om.google.android.gms:play -services:7.0.0'
testCompile(org.robolectric:robolectric:3.0-rc2){
排除模块:'commons-logging'
排除模块:'httpclient'
$ b排除模块:'commons-logging'
排除模块:'httpclient'
$ b testcompile(org.robolectric:shadows-play-services:3.0-rc2)
$ b testCompile'c​​om.squareup.okhttp:mockwebserver:1.2.1'

我从我的项目运行一个方法来从API获取json。当调用它时,我传递了广告ID:

  AdvertisingIdClient.Info adInfo = null; 
尝试{
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
} catch(IOException |
GooglePlayServicesNotAvailableException |
GooglePlayServicesRepairableException e){
//连接到Google Play服务的不可恢复的错误(例如,
//旧版本的服务不支持获取AdvertisingId)。
e.printStackTrace();
}

我测试的主要部分是:

  @Test(timeout = 7000)
public void jsonLoading(){
try {
client.loadData(this);
} catch(Exception e){
ex = e;
}

assertNull(ex);

//等待任务代码
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()抛出异常{
//关闭Google Analytics - 不需要再工作
final ShadowApplication shadowApplication = Shadows.shadowOf(RuntimeEnvironment.application);
shadowApplication.declareActionUnbindable(com.google.android.gms.analytics.service.START);

//强制成功
ShadowGooglePlayServicesUtil.setIsGooglePlayServicesAvailable(ConnectionResult.SUCCESS);
}

@
之后public void tearDown()抛出异常{

}
}

public MyTestClass扩展TestBase {

}


I've just started to use Robolectric and wanted to know how to resolve Google Play Services. I'm using Robolectric 3 RC2, and my gradle is as follow :

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'

I'm running a method from my project to get json from an API. When calling it, I pass the advertising 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();
        }

The main part of my test is :

@Test(timeout = 7000)
public void jsonLoading() {
    try {
        client.loadData(this);
    } catch (Exception e){
        ex = e;
    }

    assertNull(ex);

    //wait for task code
    ShadowApplication.runBackgroundTasks();

Each time I run the test, I've got the GooglePlayServicesNotAvailableException (from e.printStackTrace();) and I'm unable to solve it AND to assert it so the test will not pass.

I've haven't find any clue to debug my code.

解决方案

I ran into this the other day.

Use:

testCompile 'org.robolectric:shadows-play-services:3.0'
testCompile 'org.robolectric:shadows-support-v4:3.0'

And see this example:

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 {

}

这篇关于Robolectric 3 GooglePlayServicesNotAvailableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆