android – Robolectric与新版Google Play服务的问题

前端之家收集整理的这篇文章主要介绍了android – Robolectric与新版Google Play服务的问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用Robolectric进行单元测试,我的项目中有Google Play服务.直到昨天,当Google Play服务更新为新版本时,此工作正常.我收到此错误
  1. java.lang.NullPointerException
  2. at com.google.android.gms.common.GooglePlayServicesUtil.zzh(Unknown Source)
  3. at com.google.android.gms.common.GooglePlayServicesUtil.zzd(Unknown Source)
  4. at com.google.android.gms.common.GoogleApiAvailability.isGooglePlayServicesAvailable(Unknown Source)
  5. at com.google.android.gms.common.api.zzg$zze.zznn(Unknown Source)
  6. at com.google.android.gms.common.api.zzg$zzi.run(Unknown Source)
  7. at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  8. at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  9. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  10. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  11. at java.lang.Thread.run(Thread.java:745)
  12.  
  13. Process finished with exit code 255

似乎没有调用Shadow类,调用GooglePlayServicesUtil给出NullPointerException.有没有人见过这个?

我甚至不在测试中使用Google Play服务.

解决方法

添加了下一个解决方法,它工作正常:

>将所有PlayServices的相关代码解压缩到Utility类(在我的例子中,它只是可用性检查):

  1. public class PlayServicesUtils {
  2.  
  3. private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
  4.  
  5. public static final int AVAILABLE = 1;
  6. public static final int ERROR_RESOLVABLE = 2;
  7. public static final int ERROR_UNRESOLVABLE = 3;
  8.  
  9. @IntDef({AVAILABLE,ERROR_RESOLVABLE,ERROR_UNRESOLVABLE})
  10. @Retention(RetentionPolicy.SOURCE)
  11. public @interface PlayServicesAvailability {
  12. }
  13.  
  14. @PlayServicesAvailability
  15. public static int checkPlayServices(@NonNull Activity activity) {
  16. GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
  17. int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
  18. if (resultCode != ConnectionResult.SUCCESS) {
  19. if (apiAvailability.isUserResolvableError(resultCode)) {
  20. apiAvailability.getErrorDialog(activity,resultCode,PLAY_SERVICES_RESOLUTION_REQUEST).show();
  21. return PlayServicesUtils.ERROR_RESOLVABLE;
  22. } else {
  23. CLog.e(Constants.TAG,"This device does not support Google Play services.");
  24. return PlayServicesUtils.ERROR_UNRESOLVABLE;
  25. }
  26. }
  27. return PlayServicesUtils.AVAILABLE;
  28. }
  29. }

>为此Utility类实现阴影:

  1. @Implements(PlayServicesUtils.class)
  2. public class ShadowPlayServicesUtils {
  3.  
  4. @Implementation
  5. @PlayServicesUtils.PlayServicesAvailability
  6. public static int checkPlayServices(@NonNull Activity activity) {
  7. return PlayServicesUtils.AVAILABLE;
  8. }
  9. }

>为您的测试类(或基类测试类)添加阴影:

  1. @Ignore
  2. @RunWith(TestRunner.class)
  3. @Config(
  4. sdk = 18,constants = BuildConfig.class,shadows = {
  5. ShadowPlayServicesUtils.class
  6. }
  7. )
  8. public abstract class BaseTest {
  9. // some code,maybe
  10. }

>将您的影子添加到TestRunner的InstrumentationConfiguration创建中:

  1. public class TestRunner extends RobolectricGradleTestRunner {
  2. public TestRunner(Class<?> klass) throws InitializationError {
  3. super(klass);
  4. }
  5.  
  6. @Override
  7. public InstrumentationConfiguration createClassLoaderConfig() {
  8. InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
  9.  
  10. builder.addInstrumentedClass(PlayServicesUtils.class.getName());
  11.  
  12. return builder.build();
  13. }
  14. }

原始答案:

我在Robolectric问题跟踪器上找到了similar issue,并在那里提供了解决方法 – 工作!

只需强制成功初始化Google Play服务:

  1. @Before public void setUp() {
  2. // force success every time
  3. ShadowGooglePlayServicesUtil.setIsGooglePlayServicesAvailable(ConnectionResult.SUCCESS);
  4. }

编辑:

但是还有另一个issue Play Services 8.3和8.4.这个问题仍然没有解决.

猜你在找的Android相关文章