【问题标题】:Robolectric tests fail on shadowOf在 shadowOf 上 Robolectric 测试失败
【发布时间】:2013-06-10 16:47:40
【问题描述】:

可能是什么问题?我的 Robolectric 测试不久前运行良好,但现在它们都因相同的错误而失败。我能想到的唯一更改是更改为 2.1.1 版本。我正在使用 RobolectricTestRunner 运行它们,并且运行配置指向实际的应用程序目录。

但每次测试都失败了,因为应用程序的应用程序类在访问首选项时始终为空:

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)

【问题讨论】:

    标签: android robolectric


    【解决方案1】:

    显然在我的应用程序类的 onCreate 中发生了错误:

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

    getBaseContext() 更改为 this 并且测试再次工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多