可能是什么问题?我的机器人电气测试前一段时间运行良好,但现在它们都失败了,出现了同样的错误。我能想到的唯一改变就是改成2.1.1版本。我用robolectrictestrrunner运行它们,运行配置指向实际的应用目录。
但是每次测试都失败,因为应用程序的应用程序类总是空的,因为首选项正在被访问:
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)
显然,错误发生了,因为在我的应用程序类的onCreate:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
将getBaseContext()更改为此,测试再次工作。