首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Robolectric 3 GooglePlayServicesNotAvailableException

Robolectric 3 GooglePlayServicesNotAvailableException
EN

Stack Overflow用户
提问于 2015-04-28 09:42:10
回答 3查看 964关注 0票数 3

我刚刚开始使用Robolectric,并想知道如何解决、Google服务。我使用的是Robolectric 3 RC2,我的分级如下:

build.bradle

代码语言:javascript
复制
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:

代码语言:javascript
复制
        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();
        }

我考试的主要部分是:

代码语言:javascript
复制
@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();),并且无法通过解决断言,这样测试就不会通过。

我还没有找到任何线索来调试我的代码。

EN

回答 3

Stack Overflow用户

发布于 2015-07-26 01:41:06

前几天我碰到了这个。

使用:

代码语言:javascript
复制
testCompile 'org.robolectric:shadows-play-services:3.0'
testCompile 'org.robolectric:shadows-support-v4:3.0'

看看这个例子:

代码语言:javascript
复制
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 {

}
票数 3
EN

Stack Overflow用户

发布于 2018-02-21 16:31:43

对于试图使用GoogleApiAvailability的人来说.

gradle

代码语言:javascript
复制
dependencies {
  testApi 'org.robolectric:robolectric:3.6.1'
  testApi 'org.robolectric:shadows-playservices:3.6.1'
}

代码语言:javascript
复制
public class TestBase {

    @Before
    public void setUp() throws Exception {
        final ShadowGoogleApiAvailability shadowGoogleApiAvailability
                = Shadows.shadowOf(GoogleApiAvailability.getInstance());
        shadowGoogleApiAvailability.setIsGooglePlayServicesAvailable(ConnectionResult.SUCCESS);
    }

    @After
    public void tearDown() throws Exception {
    }
}

public MyTestClass extends TestBase {
}
票数 1
EN

Stack Overflow用户

发布于 2015-05-01 21:02:06

我不认为你可以使用谷歌播放服务与频密,你将需要阴影。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29915790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档