Robolectric测试失败的shadowOf [英] Robolectric tests fail on shadowOf

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

问题描述

可能是什么问题?我Robolectric测试运行正常前一阵子,但现在他们都失败,并同样的错误。我能想到的唯一的变化正在改变2.1.1版本。我与RobolectricTestRunner和运行配置指向实际应用目录中运行它们。

但由于应用程序的应用程序类总是空为preferences被访问的每一个测试失败:

 显示java.lang.NullPointerException:不能得到空阴影
        在org.robolectric.byte code.ShadowWrangler.shadowOf(ShadowWrangler.java:414)
        在org.robolectric.Robolectric.shadowOf_(Robolectric.java:1077)
        在org.robolectric.Robolectric.shadowOf(Robolectric.java:483)
        在org.robolectric.shadows.Shadow$p$pferenceManager.getDefaultShared$p$pferences(Shadow$p$pferenceManager.java:21)
        在Android系统。preference。preferenceManager.getDefaultShared preferences(preferenceManager.java)
        在com.foo.bar.FooApplication.onCreate(FooApplication.java:90)


解决方案

显然发生了错误,因为这在我的应用程序类的OnCreate:

 共享preferences preFS = preferenceManager.getDefaultShared preferences(getBaseContext());

改变了的 getBaseContext()的到的这个的和测试工作了。

What might be the problem? My Robolectric tests ran fine a while ago, but now they all fail with the same error. The only change I can think of is changing to version 2.1.1. I'm running them with the RobolectricTestRunner and the run configuration points to the actual app directory.

But every test fails because the app's application class is always null as preferences are being accessed:

java.lang.NullPointerException: can't get a shadow for null
        at org.robolectric.bytecode.ShadowWrangler.shadowOf(ShadowWrangler.java:414)
        at org.robolectric.Robolectric.shadowOf_(Robolectric.java:1077)
        at org.robolectric.Robolectric.shadowOf(Robolectric.java:483)
        at org.robolectric.shadows.ShadowPreferenceManager.getDefaultSharedPreferences(ShadowPreferenceManager.java:21)
        at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java)
        at com.foo.bar.FooApplication.onCreate(FooApplication.java:90)

解决方案

Apparently the error occurred because of this in the onCreate of my application class:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

Changed the getBaseContext() to this and the tests work again.

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

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