我刚刚开始使用Robolectric,并想知道如何解决、Google服务。我使用的是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();),并且无法通过解决和断言,这样测试就不会通过。
我还没有找到任何线索来调试我的代码。
发布于 2015-07-26 01:41:06
前几天我碰到了这个。
使用:
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 {
}发布于 2018-02-21 16:31:43
对于试图使用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 {
}发布于 2015-05-01 21:02:06
我不认为你可以使用谷歌播放服务与频密,你将需要阴影。
https://stackoverflow.com/questions/29915790
复制相似问题