簡體   English   中英

Robolectric 3 GooglePlayServicesNotAvailableException

[英]Robolectric 3 GooglePlayServicesNotAvailableException

我剛剛開始使用Robolectric,想知道如何解決Google Play服務 我正在使用Robolectric 3 RC2 ,我的gradle如下:

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 {

}

對於嘗試在Robolectric中使用GoogleApiAvailability的人...

gradle這個

dependencies {
  testApi 'org.robolectric:robolectric:3.6.1'
  testApi 'org.robolectric:shadows-playservices:3.6.1'
}

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 {
}

我不認為你可以使用robolectric使用谷歌播放服務,你需要陰影。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM