简体   繁体   English

Robolectric测试在shadowOf上失败

[英]Robolectric tests fail on shadowOf

What might be the problem? 可能是什么问题? My Robolectric tests ran fine a while ago, but now they all fail with the same error. 我的Robolectric测试前一阵子运行良好,但现在它们都因相同的错误而失败。 The only change I can think of is changing to version 2.1.1. 我能想到的唯一更改是更改为2.1.1版。 I'm running them with the RobolectricTestRunner and the run configuration points to the actual app directory. 我使用RobolectricTestRunner运行它们,运行配置指向实际的应用程序目录。

But every test fails because the app's application class is always null as preferences are being accessed: 但是每次测试都失败,因为在访问首选项时,应用程序的应用程序类始终为null:

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: 显然是由于这个原因导致了我的应用程序类的onCreate错误:

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

Changed the getBaseContext() to this and the tests work again. getBaseContext()更改为此 ,测试将再次工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM